Click here to Skip to main content
12,951,983 members (61,861 online)
Rate this:
Please Sign up or sign in to 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:
// 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:


How do I do this?
Posted 13-Nov-12 11:51am
Updated 13-Nov-12 21:34pm
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)
That return the first index of searched string.
Be successful.
Not exactly true. Actually, methods do not have indices, which would have no use. But you can find everything from Reflection.
Why, why?!
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.
werfog 14-Nov-12 7:21am
I would do that next way.
Surround MyMethod with try-catch and
catch(Exception ex) { MessageBox("MyMethod's StringNumber"); }
That shows the user necessary information.
If I understand you correctly.
Best wishes.
werfog 14-Nov-12 8:24am
you may find something useful.
jmh1 14-Nov-12 8:53am
werfog: The try-catch and MessageBox you sugest does not work for me. And i have no problem finding documentation for Regex. But thank you anyway!

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:[^].

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…

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...
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...


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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,239
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880

Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100