Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:
C#
// 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;
        }

    }
}


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?
Posted
Updated 13-Nov-12 21:34pm
v3
Comments
werfog 13-Nov-12 22:41pm    
If the code has been already compiled I guess that is impossible to find method position in code.
If you have just document aaa.cs. You can do something like that:
String s = ... (get text from document)
s.IndexOf("MyMethod");
That return the first index of searched string.
Be successful.
Sergey Alexandrovich Kryukov 14-Nov-12 0:52am    
Not exactly true. Actually, methods do not have indices, which would have no use. But you can find everything from Reflection.
--SA
Sergey Alexandrovich Kryukov 14-Nov-12 0:51am    
Why, why?!
--SA
werfog 14-Nov-12 6:34am    
Do you want to debug your code on your own or you want user to debug that?
jmh1 14-Nov-12 6:50am    
werfog: The user writes the script code which controls how parts of the app functions. So the user has to debug his own code. When the app knows the line number of the failing code, the user can doubleclick the error in the Error List and will be transferred to the script code that failed, like in Visual Studio. It is also possible to break script code and activate the CLR debugger, but that is something entirely different.

1 solution

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
 
Share this answer
 
v2
Comments
jmh1 14-Nov-12 3:35am    
I guess the first version of my my question wasn't clear enough. I want to blame that on the late hour and the Regex headache. The app i am working on is using C# as an embedded scripting language and i need to find the line number in the sourcefile of the method causing a runtime error. Please see my edited question above.
Sergey Alexandrovich Kryukov 14-Nov-12 11:28am    
I don't think you will get any advice before you explain why would you need it. Not many will waste time discussing the technique for some questionable or mysterious purpose...
--SA
jmh1 19-Nov-12 7:45am    
Mysterious purpose? Sorry Sergey, i did'n realize i had to explain the whole app to get help. Looking around, most people that get help do not explain very much at all about their apps. Just forget it Sergey, i will as usual have to solve it myself.
Sergey Alexandrovich Kryukov 19-Nov-12 11:35am    
I don't think you understand. Some 99% of questions these days deserve removing them at once, but usually we try to give inquirers a chance to improve question.
Your question is much better then those though, so you should not look around and compare other questions with yours. The only reasonable criteria is related to how you can get help, do you provide adequate information or not. You could imagine that you are the one who is supposed to answer and think, would your question be good enough for you to answer, assuming you have no other information.

And "explain the whole app" would be a bad idea. In most cases, the really good idea is this: 1) explain your ultimate goal, 2) if you have some idea on the technical means you want to use, motivate it -- it can be good or not; always give people a chance to give you a better idea; 3) don't send too big code, it's the best to create a complete but short code sample, specially for asking a question, focusing of just one problem; I understand it's not always easy or even possible. Plus, there are apparent piece of information to be commented on the code.

Now, your particular problem is not about some implementation -- it's about architecture and design. The problem of creating some host for embedded scripting can be very interesting and non-trivial, but I suspect you are trying to come to solving it from wrong direction. .NET programming is not working in the way you picture; and the indices of the methods do not exist in principle. You need some other approach.

After all, I actually created some scripting hosts and related technologies, so I know what I am talking about. There are very neat and powerful approached, but I could only shared it with you if you explained your ultimate goals and related ideas. It's not too late, by the way...

--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900