在C#源文件中查找方法的字符索引. [英] Finding character index of method in C# source file.

查看:72
本文介绍了在C#源文件中查找方法的字符索引.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用CS-Script作为编译器接口的应用程序中使用C#作为脚本语言.当脚本导致运行时错误时,异常堆栈跟踪将包含有关导致异常的方法的名称空间,类和方法名的信息.

我的字符串中包含源代码,该字符串包含不同名称空间和类中的许多方法.

检测到运行时错误,我已经解析出发生错误的名称空间,类和方法名称.现在,我需要在声明失败方法的源字符串中找到字符索引.有了该索引,我可以找到行号,然后可以告诉用户错误发生的位置.

可以说代码看起来像这样:

I am using C# as a scripting language i my app with CS-Script as the compiler interface. When a script causes a runtime error, the exception stacktrace contains information on the namespace, class and methodname of the method causing the exception.

I have the source code in a string which contains many methods in different namespaces and classes.

Detecting a runtime error, i have parsed out namespace, class and method name where the error occured. Now i need to find the character index in the source string where the failing method is declared. Having that index, i can find the line number and can then tell the user where the error occurred.

Lets say the code looks like this:

// Other code blocks with same or other namespace and same or other class names might
// precede the code.
// The Regex should not find MyMethod if it is not within MyNamespace and MyClass. 
 
namespace MyNamespace {
    public static class MyClass {

        public static int OtherMethod(int val) {
            return val * 2;
        }

        public static int MyMethod(int val) {
            return 10 / val;
        }

    }
}



已知的是名称空间是MyNamespace,类是MyClass,而我们要查找的方法名称是MyMethod.

我需要找出的是该名称空间和类中的标识符MyMethod在代码中的第y行声明.

我试图制作一个C#Regex表达式,但是我无法使其正常工作,现在这些Regex象形文字开始让我头疼不已.我想到的是这样的:

.* namespace \ s + MyNamespace.* class \ s + MyClass.* MyMethod \ s * \("

我该怎么做?



What is known is that the namespace is MyNamespace, that the class is MyClass and that the method name we are looking for is MyMethod.

What i need to find out is that the identifier MyMethod within this namespace and class is declared at line y in the code.

I have tried to make a C# Regex expression, but i can''t make it work and now these Regex hieroglyphs are beginning to give me a bad headache. What i came up with is this:

".*namespace\s+MyNamespace.*class\s+MyClass.*MyMethod\s*\("

How do I do this?

推荐答案

即使您可以找到某种可以理解的方法索引,也绝对可以不使用它.此外,按方法名称查找所有内容的想法在原则上是不好的.

要获得对相关主题的一些理解,您确实需要学习反思:
http://msdn.microsoft.com/en-us/library/f7ykdhsy.aspx [ ^ ].

实际上,您可以找到有关每个已编译程序集中每个方法的所有信息.但是,在大多数情况下,就其维护而言,基于字符串信息(例如方法名称,类型,属性等)查找任何元数据将使代码变得非常糟糕.

真正好的方法是实现某些接口,与主机程序集共享接口定义(您可以在主机程序集中定义该接口,然后通过动态加载的程序集引用该程序集,因为引用入口程序集或应用程序程序集没有错( EXE);我不确定您是否有足够的知识来理解此部分,但您始终可以提出后续问题)并通过其实现的接口查找类型.如果完成此操作,则可以使用Reflection实例化类型,然后直接使用所有接口方法.

我不想仅仅因为不想浪费时间而提供更多细节-我不知道您的预期目的.就是说,您应该从您的最终目标开始所有问题.暂时,您根本不需要反射或其他高级的东西……

—SA
Even if you could find something which would be an "index" of a method in the sense you understand it, it would be absolutely no use of it. Besides, the idea to find anything at all by method name is bad in principle.

To get some understanding on the related topics, you really need to learn Reflection:
http://msdn.microsoft.com/en-us/library/f7ykdhsy.aspx[^].

You can actually find out everything about each method in each compiled assembly. However, finding any metadata based on string information such as the name of method, type, property, etc., would make really bad code, in terms of its maintenance, well, in most cases.

A really good approach is to implement some interface, share the interface definition with host assembly (you can defined it in a host assembly and reference this assembly by dynamically loaded assemblies, because there is nothing wrong in referencing the entry assembly, or application assembly (EXE); I''m not sure you have enough knowledge to understand this part, but you can always ask your follow-up questions) and find the type by the interface it implements. If this is done, you can instantiate the type using Reflection and then directly use interface methods, all of them.

I don''t provide further detail just because I don''t want to waste time — I have no idea of your intended purpose. That said, you should start all your questions with your ultimate goals. Chances are, you won''t need Reflection or other advanced stuff at all, for the time being…

—SA


这篇关于在C#源文件中查找方法的字符索引.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆