bryanedds

In java, I believe you can attempt to call a method even though the compiler can't find the method in the object's known class. Basically, you could do something like this -

Object o;

o.SomeMethod();

The java compiler doesn't look to see if SomeMethod exists in o. It simply compiles the code and tries to find the method on the object at run-time. If it's not found, something like a MethodNotFoundException is thrown, and can be handled gracefully.

I want to do precisely the same thing in C# without using reflection. I think this type of behavior can be acheived in a roundabout way using the item of a foreach loop, but I want to be able to do it anywhere. Essentially, I want to be able to get this piece of code to compile -

try

{

Object o = new Object();

o.NonexistantMethod();

}

catch (Exception) { }

Can anyone help me out here




Re: Visual C# Language Completely dynamic method invocation

theblueeyz

Hmm.. to understand how to answer your question, it might be helpful to understand what you're really trying to do.

For example, the way the event model works does exactly what you want, in a roundabout sort of way:

someObj.SomeEvent += SomeHandler(yourMethodName);

You're giving it the method to call. It has NO idea what that method is, and invokes it blindly. The compiler does check the method signature, but there are ways around that.

So what is your real purpose in wanting blind method invokation There are ways to do this but the pattern sort of depends on how you'll use it.





Re: Visual C# Language Completely dynamic method invocation

bryanedds

What I'm trying to do is a little weird.

I'm building a console system which allows the user to execute C# code that he types into an in-game console GUI while the program is running.

Basically, I have programmed a static method called Execute. It takes a string of C# code as a parameter as well as a generic Object of a known type.

It takes the passed C# code and surrounds it with the necessary code compile into a static function inside an assembly in memory. That function is then invoked via reflection, thus finally executing the code that the user entered into the console.

The reason I want to be able to blindly invoke methods is this -

Remember the generic Object that is passed into the Execute function that I spoke about It should be able to have methods called on it without being explicitly casted to it's actual type. It's somewhat like a scripting language feature. If there is a way to do blind invocation on a chunk of code, I want to be able to do it in the code generated by the Execute function.

This probably sounds hard to understand, but it's just a real-time interactive console. If you've ever used TGE, TSE, or TGB, you'll know what I'm talking about for sure :)






Re: Visual C# Language Completely dynamic method invocation

JDPeckham

Why don't you make an interface with an Execute() method that all console entered (runtime compiled) code implements (like a Main() of a console program for your console entered scripts)

 

I know it's not dynamic (as in they get to choose their own starting point for the code, but i think that would get what you want without using reflection)

 

at run time you would let them enter their code (which would have to implement Execute() (or it wouldn't  compile, throwing an error) compile your assembly, Create and instance of their class that implements your execute interface, pass it to your program using a reference to your interface, then call Execute() on it...

 

am i anywhere near what you're thinking

 

Are they basically entering in code, it compiles, then they enter in scripts that calls those methods






Re: Visual C# Language Completely dynamic method invocation

RizwanSharp

I don't know how yo answer but we are going in the opposite direction of what you are thinking with the innovation of Generics in .Net 2.0 + because there is a cost related to Runtime object casting which can better be done ar compile time so your code must be more optimised at run time.

What java people has implemented is seemed like a flexibility in language but there is a cost of boxing/unboxing then at runtime besides running a method its going to find that method . Adding an overhead to the code execution. Second, It'll also lead to us to error prone code, Developer thinks that AClass has a function AFunction because it compiles but but at runtime a MethodNotFoundException is thrown and developer needs to change all code in the pplication he has written so far .

If you really want to do it that way I would recomend using Abstract classes, Just inherit all you classes from one base abstract class and pass this where you want.

Here

public abstract class SomeClass

{

public abstract intAdd(int i, int j);

public abstract intSubtract(int i, int j);

}

not Derived classses:

public class SomeClass1

{

public override int Add(int i, int j)

{

return i + j;

}

public override int Subtract(int i, int j)

{

return i - j;

}

}

and other one

public class SomeClass1

{

public override int Add(int i, int j)

{

throw new NotImplementedException("This method is not implemented");

}

public override int Subtratct(int i, int j)

{

throw new NotImplementedException("This method is not implemented"); // Or create your own MethodNotFoundException by inheriting from Exception class

}

}

I hope you know what to do next.

Cheers ,

Best Regards,

Rizwan aka RizwanSharp






Re: Visual C# Language Completely dynamic method invocation

boban.s

Don't you think that is better to give the user some editor where he can overload some alredy implemented methods in your assembly, in which editor when he will do that, you will give him intellisense feature. I use ICSharpCode.TextEditor control for such thing. I know, giving the intellisence to the user is hard and big job. But you will help the user discover the methods, method parameters, types, properties and many more. Also after he will write the overloads you will build that source in a new assembly and if you build succesfuly ok, if not you will inform the user that his source is not correct.
About a question why C# doesn't have such posibility. It is because C# is typed language. And this can be great feature for many of developers, but for just small number of them is a luck of posibility.
But if you still need that as a posibility, using reflection can you can have that. Yes, you can invoke assembly methods by name using InvokeMember method. This will give you a posibility to not have typed code, but will not help in discovering that that method realy exists and that the job from invoking this method is finished.