Of course First, you must get the type of your class. Now I give you a sample. There is a Form1, if you want to add a dynamic method in the Form1 when the assembly is running. You may use Reflection Emit API to do that.The method we will add is : public String Method(String str) { return "Hello:" + str; }The method will be added to the Form1 when the assembly containing the Form1 is running. Then it will be run.step0: you must use one namespace: using System.Reflection.Emit; step1: declare a delegatepublic delegate String MyMethod(String str);step2: create a dynamic method Type[] argsType = { typeof(String) }; // argument type of the MyMethod1 DynamicMethod dm = new DynamicMethod("MyMethod1", typeof(String), argsType, typeof(Form1).Module);step3: writing the code of the MyMethod1( note: IL(Intermedial language must be used)ILGenerator il = dm.GetILGenerator(); // get the IL generator of the MyMethod1il.Emit(OpCodes.Ldstr, "hello:"); // push the "hello:" string onto the evaluation stack il.Emit(OpCodes.Ldarg_0); // push the first argument of the MyMethod1 onto the evaluation stack // call the Concat method of the String class (it is static) il.Emit(OpCodes.Call, typeof(String).GetMethod( "Concat", new Type[]{typeof(String), typeof(String)})); il.Emit(OpCodes.Ret); // return the "Hello:" + str;step4: show the return value MyMethod my = (MyMethod)dm.CreateDelegate(typeof(MyMethod)); MessageBox.Show(my("my friends"));Ok, you succeed!An alternative is Codedom. You can use source code here. The information about codedom will be find on msdn(online or offline).good luck.
f course First, you must get the type of your class. Now I give you a sample. There is a Form1, if you want to add a dynamic method in the Form1 when the assembly is running. You may use Reflection Emit API to do that.The method we will add is : public String Method(String str) { return "Hello:" + str; }The method will be added to the Form1 when the assembly containing the Form1 is running. Then it will be run.step0: you must use one namespace: using System.Reflection.Emit; step1: declare a delegatepublic delegate String MyMethod(String str);step2: create a dynamic method Type[] argsType = { typeof(String) }; // argument type of the MyMethod1 DynamicMethod dm = new DynamicMethod("MyMethod1", typeof(String), argsType, typeof(Form1).Module);step3: writing the code of the MyMethod1( note: IL(Intermedial language) must be used)ILGenerator il = dm.GetILGenerator(); // get the IL generator of the MyMethod1il.Emit(OpCodes.Ldstr, "hello:"); // push the "hello:" string onto the evaluation stack il.Emit(OpCodes.Ldarg_0); // push the first argument of the MyMethod1 onto the evaluation stack // call the Concat method of the String class (it is static) il.Emit(OpCodes.Call, typeof(String).GetMethod( "Concat", new Type[]{typeof(String), typeof(String)})); il.Emit(OpCodes.Ret); // return the "Hello:" + str;step4: show the return value MyMethod my = (MyMethod)dm.CreateDelegate(typeof(MyMethod)); MessageBox.Show(my("my friends"));Ok, you succeed!An alternative is Codedom. You can use source code here. The information about codedom will be found on msdn(online or offline).
Of course First, you must get the type of your class. Now I give you a sample. There is a Form1, if you want to add a dynamic method in the Form1 when the assembly is running. You may use Reflection Emit API to do that.The method we will add is : public String Method(String str) { return "Hello:" + str; }The method will be added to the Form1 when the assembly containing the Form1 is running. Then it will be run.step0: you must use one namespace: using System.Reflection.Emit; step1: declare a delegatepublic delegate String MyMethod(String str);step2: create a dynamic method Type[] argsType = { typeof(String) }; // argument type of the MyMethod1 DynamicMethod dm = new DynamicMethod("MyMethod1", typeof(String), argsType, typeof(Form1).Module);step3: writing the code of the MyMethod1( note: IL(Intermedial language) must be used)ILGenerator il = dm.GetILGenerator(); // get the IL generator of the MyMethod1il.Emit(OpCodes.Ldstr, "hello:"); // push the "hello:" string onto the evaluation stack il.Emit(OpCodes.Ldarg_0); // push the first argument of the MyMethod1 onto the evaluation stack // call the Concat method of the String class (it is static) il.Emit(OpCodes.Call, typeof(String).GetMethod( "Concat", new Type[]{typeof(String), typeof(String)})); il.Emit(OpCodes.Ret); // return the "Hello:" + str;step4: show the return value MyMethod my = (MyMethod)dm.CreateDelegate(typeof(MyMethod)); MessageBox.Show(my("my friends"));Ok, you succeed!An alternative is Codedom. You can use source code here. The information about codedom will be found on msdn(online or offline).good luck.
TO bughole(虫洞)您的这个例子是动态生成一个方法并绑给一个委托是吗?虽然我没有试过,但还是蛮值得学习的。btw:如何加一个member呢?
TO lovewindy(LOVE风云)谢谢, 设计模式读过一点,了解很肤浅; 您可否再解释详细一点? 谢谢!
What's member? property? or others
Method is also member method. Member contains methods, property, field and so on. You say which it is.
你可以放一个ArrayList成员,对ArrayList进行增删`
这个问题源自面向对象(OO)开发中的一个思考。对象A拥有若干[属性](attr1,attr2,...),注意这里的属性可能是个有意义的对象;
现在要求在运行时,动态添加A的成员。后来在项目实现时我还是采用了动态存储,动态加载这样的机制使得看起来像OO;请恕在下才疏学浅,对OO的理解真的不够深入;
想借机听听各位大侠的意见~~~~谢谢以上各位大侠的支持^_^
但是如果是要在运行时来给已经编译过的类添加一个成员的话,恕我孤陋寡闻,不行
你可以去看一下Decorator Pattern
需要动态地给一个对象增加功能,这些功能可以再动态地撤销,这就是它的一个作用.
First, you must get the type of your class. Now I give you a sample.
There is a Form1, if you want to add a dynamic method in the Form1 when the assembly is running. You may use Reflection Emit API to do that.The method we will add is :
public String Method(String str)
{
return "Hello:" + str;
}The method will be added to the Form1 when the assembly containing the Form1 is running. Then it will be run.step0: you must use one namespace: using System.Reflection.Emit; step1: declare a delegatepublic delegate String MyMethod(String str);step2: create a dynamic method Type[] argsType = { typeof(String) }; // argument type of the MyMethod1
DynamicMethod dm = new DynamicMethod("MyMethod1", typeof(String), argsType, typeof(Form1).Module);step3: writing the code of the MyMethod1( note: IL(Intermedial language must be used)ILGenerator il = dm.GetILGenerator(); // get the IL generator of the MyMethod1il.Emit(OpCodes.Ldstr, "hello:"); // push the "hello:" string onto the evaluation stack
il.Emit(OpCodes.Ldarg_0); // push the first argument of the MyMethod1 onto the evaluation stack
// call the Concat method of the String class (it is static)
il.Emit(OpCodes.Call, typeof(String).GetMethod( "Concat", new Type[]{typeof(String), typeof(String)}));
il.Emit(OpCodes.Ret); // return the "Hello:" + str;step4: show the return value
MyMethod my = (MyMethod)dm.CreateDelegate(typeof(MyMethod));
MessageBox.Show(my("my friends"));Ok, you succeed!An alternative is Codedom. You can use source code here. The information about codedom will be find on msdn(online or offline).good luck.
First, you must get the type of your class. Now I give you a sample.
There is a Form1, if you want to add a dynamic method in the Form1 when the assembly is running. You may use Reflection Emit API to do that.The method we will add is :
public String Method(String str)
{
return "Hello:" + str;
}The method will be added to the Form1 when the assembly containing the Form1 is running. Then it will be run.step0: you must use one namespace: using System.Reflection.Emit; step1: declare a delegatepublic delegate String MyMethod(String str);step2: create a dynamic method Type[] argsType = { typeof(String) }; // argument type of the MyMethod1
DynamicMethod dm = new DynamicMethod("MyMethod1", typeof(String), argsType, typeof(Form1).Module);step3: writing the code of the MyMethod1( note: IL(Intermedial language) must be used)ILGenerator il = dm.GetILGenerator(); // get the IL generator of the MyMethod1il.Emit(OpCodes.Ldstr, "hello:"); // push the "hello:" string onto the evaluation stack
il.Emit(OpCodes.Ldarg_0); // push the first argument of the MyMethod1 onto the evaluation stack
// call the Concat method of the String class (it is static)
il.Emit(OpCodes.Call, typeof(String).GetMethod( "Concat", new Type[]{typeof(String), typeof(String)}));
il.Emit(OpCodes.Ret); // return the "Hello:" + str;step4: show the return value
MyMethod my = (MyMethod)dm.CreateDelegate(typeof(MyMethod));
MessageBox.Show(my("my friends"));Ok, you succeed!An alternative is Codedom. You can use source code here. The information about codedom will be found on msdn(online or offline).
First, you must get the type of your class. Now I give you a sample.
There is a Form1, if you want to add a dynamic method in the Form1 when the assembly is running. You may use Reflection Emit API to do that.The method we will add is :
public String Method(String str)
{
return "Hello:" + str;
}The method will be added to the Form1 when the assembly containing the Form1 is running. Then it will be run.step0: you must use one namespace: using System.Reflection.Emit; step1: declare a delegatepublic delegate String MyMethod(String str);step2: create a dynamic method Type[] argsType = { typeof(String) }; // argument type of the MyMethod1
DynamicMethod dm = new DynamicMethod("MyMethod1", typeof(String), argsType, typeof(Form1).Module);step3: writing the code of the MyMethod1( note: IL(Intermedial language) must be used)ILGenerator il = dm.GetILGenerator(); // get the IL generator of the MyMethod1il.Emit(OpCodes.Ldstr, "hello:"); // push the "hello:" string onto the evaluation stack
il.Emit(OpCodes.Ldarg_0); // push the first argument of the MyMethod1 onto the evaluation stack
// call the Concat method of the String class (it is static)
il.Emit(OpCodes.Call, typeof(String).GetMethod( "Concat", new Type[]{typeof(String), typeof(String)}));
il.Emit(OpCodes.Ret); // return the "Hello:" + str;step4: show the return value
MyMethod my = (MyMethod)dm.CreateDelegate(typeof(MyMethod));
MessageBox.Show(my("my friends"));Ok, you succeed!An alternative is Codedom. You can use source code here. The information about codedom will be found on msdn(online or offline).good luck.
设计模式读过一点,了解很肤浅;
您可否再解释详细一点?
谢谢!