Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello,

I want to create a method on RunTime.
I want the user to enter a string and the method's name will be DynamicallyDefonedMethod_#### (ends with the user string).

i want the same string to be embeded in the method's body:
It will call StaticallyDefinedMethod (####, a, b)

Something like:
C#
public MyClass1 DynamicallyDefonedMethod_#### (int a, int b)
{
return  StaticallyDefinedMethod (####, a, b)
}


The idea is that the user will create a new method on runtime and invoke it afterward (with a, b parameters).

I googled C# reflection but found no easy way of doing that. Does someone knows how to do it simply ?


Is it possible to define that new method [webmethod] ?
Regards,
Posted
Updated 5-Mar-11 22:25pm
v4
Comments
imaa2amha 6-Mar-11 3:06am    
this methods should save for long time or runtime only ?
elad2109 6-Mar-11 3:08am    
runtime only.
OriginalGriff 6-Mar-11 3:12am    
Why would you want to do that? It seems rather a risky thing to do.
What are you trying to achieve that needs this?
elad2109 6-Mar-11 4:41am    
I want to create a new webMethod at runTime.
I have a user that request via POST method. I want to take his request and under some condition I want to redirect its request to a new method (useing the same POST request)
OriginalGriff 6-Mar-11 4:43am    
I understood that bit - what I don't understand is why you would want the user to be able to write code to execute on your server. It seems a little dangerous!

1 solution

Whatever you want to do with that stuff, it is not very easy. You'll need to leans a good deal.

For introduction, see this first: http://msdn.microsoft.com/en-us/library/sfk2s47t.aspx[^], then look at his type description and the code sample: http://msdn.microsoft.com/en-us/library/system.reflection.emit.dynamicmethod.aspx[^].

If you say you already saw this and still did not get the answer (remembering you phrase "I Googled C# reflection but found no easy way of doing that"), you probably have to simply slow down and stop your search: you already found what you need, now it's time to read and work it through. If you try to find anything easier than that, you will only waste time.

Cheer up!

—SA
 
Share this answer
 
Comments
elad2109 6-Mar-11 3:26am    
My only problem is how to emit the new method's body to call a statically defined method with 3 args. (no varargs method).
Sergey Alexandrovich Kryukov 6-Mar-11 3:33am    
Do you want to talk about it?
--SA
Sergey Alexandrovich Kryukov 6-Mar-11 3:37am    
Here is the idea: Write a sample assembly (as small as possible) manually instead of emitting the code, just write what you need to emit. Compile and disassemble it. It will give you a good idea how to emit. Look at code sample and do similar thing, but using your disassembled sample code.
--SA
elad2109 6-Mar-11 3:42am    
I'll try that out.
But before I do that, 2 questions:
1. can I define the new dynamic method as [WebMethod]?
2. can I Emit an instruction of putting a const value onto the stack (I want to call the statically define method with a const string as its arg0)?
Sergey Alexandrovich Kryukov 6-Mar-11 4:40am    
Pretty much sure you can do both.
--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