all groups > dotnet clr > may 2007 >
You're in the

dotnet clr

group:

Implementing Method @ Runtime


Implementing Method @ Runtime Vin=EDcius_Strasser
5/21/2007 5:38:16 AM
dotnet clr:
Hi,

I have a class that should create some methods at runtime. This method
only must call another method, already implemented, passing current
method info and parameters.

I would not like to create an assembly/class at runtime emitting il.

I've found something interesting in CompilerServices namespace.

Somebody could help me to implement this ?

Thanks.
Re: Implementing Method @ Runtime Günter Prossliner
5/21/2007 3:13:57 PM
Hello Vinícius!

[quoted text, click to view]

When you do not need anything more you want to do, the best option would be
to use classic OOP techniques like Interfaces. The caller just calls the
Interface-Function, which is implemented differently in various classes. To
create the classes you may use a kind of Factory-Pattern.

An other option would be to retrieve the MethodInfo object of the target
method dynamicly at runtime using reflection, and to call it with the
..Invoke Method (e.g. typeof(MyType).GetMethod("Name").Invoke(obj, new
object[]{"p1", "p2"});).


[quoted text, click to view]

When you use .NET 2.0 it would be possible to use the DynamicMethod, which
can emit IL-Code at Runtime without the overhead of emitting and loading an
Assembly. But if you just want to call a Method dynamicly, this seems a
little bit "oversized".


[quoted text, click to view]

And what is "something interesting"?




GP

Re: Implementing Method @ Runtime Vin=EDcius_Strasser
5/22/2007 4:56:41 AM
What I need to do, is call a method passing current method info and
parameters. The methods (that should be created at runtime) must call
their MethodInfo and current parameters.

The methods (those should be created at runtime) do not have a
contract. In really, another programmer should declare methods
signatures, as extern methods, and decorate with an attribute created
by me. At runtime this method (that until now is only a signature with
an attribute) should invoke my method passing it's method info and
parameters.

Thanks.

[quoted text, click to view]

..Invoke Method (e.g. typeof(MyType).GetMethod("Name").Invoke(obj, new
object[]{"p1", "p2"});).
Re: Implementing Method @ Runtime Jesse Houwing
5/23/2007 10:24:27 PM
* Vinícius Strasser wrote, On 22-5-2007 13:56:
[quoted text, click to view]

You should be able to use Reflection for that. Much easier than emitting
your own IL, but a little slower.

Get the Type of the class you're calling.
Use the GetMethod("name") function to fetch the MethodInfo you need
Use Invoke(instance (or null for static), parameters) to call the function.

Re: Implementing Method @ Runtime Ben Voigt
5/24/2007 1:16:38 PM

[quoted text, click to view]

It sounds more like the OP wants aspect-oriented programming, with the
ability to have a precall and postcall attachment point (forgot the right
name) with access to the identity and arguments of the method called,
perhaps to log arguments and results.

Reflection.Emit has turned this into only a moderately difficult problem,
where before it was totally beyond that grasp of anyone but an expert
compiler writer.

[quoted text, click to view]

Re: Implementing Method @ Runtime Jesse Houwing
5/24/2007 10:19:00 PM
* Ben Voigt wrote, On 24-5-2007 20:16:
[quoted text, click to view]

Ok, in that case I misunderstood the problem. :)

AddThis Social Bookmark Button