Hello Vinícius!
> 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.
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"});).
> I would not like to create an assembly/class at runtime emitting il.
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".
> I've found something interesting in CompilerServices namespace.
>
> Somebody could help me to implement this ?
And what is "something interesting"?
GP
Vinícius Strasser - 22 May 2007 12:56 GMT
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.
> Hello Vinícius!
>
[quoted text clipped - 26 lines]
>
> GP
.Invoke Method (e.g. typeof(MyType).GetMethod("Name").Invoke(obj, new
object[]{"p1", "p2"});).
Jesse Houwing - 23 May 2007 21:24 GMT
* Vinícius Strasser wrote, On 22-5-2007 13:56:
> 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
[quoted text clipped - 6 lines]
> an attribute) should invoke my method passing it's method info and
> parameters.
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.
Jesse
Ben Voigt - 24 May 2007 19:16 GMT
>* Vinícius Strasser wrote, On 22-5-2007 13:56:
>> What I need to do, is call a method passing current method info and
[quoted text clipped - 15 lines]
> Use Invoke(instance (or null for static), parameters) to call the
> function.
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.
> Jesse
Jesse Houwing - 24 May 2007 21:19 GMT
* Ben Voigt wrote, On 24-5-2007 20:16:
>> * Vinícius Strasser wrote, On 22-5-2007 13:56:
>>> What I need to do, is call a method passing current method info and
[quoted text clipped - 23 lines]
> where before it was totally beyond that grasp of anyone but an expert
> compiler writer.
Ok, in that case I misunderstood the problem. :)
Jesse