何可以创建动态方法c# [英] Ho can create dynamic method c#

查看:89
本文介绍了何可以创建动态方法c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我尝试创建动态方法



  public   void  SetId(Guid id)
{
Id = id;
}





我试试,但我是私人套装



  var  DynamicMethod =  new  DynamicMethod(  Division typeof  void ), new 输入[] { typeof (Guid)},entityType.Module ); 
var il = DynamicMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt,entityType.GetProperty( Id)。GetSetMethod ());
il.Emit(OpCodes.Ret);





我该怎么做



我尝试过:



DynamicMethod = new DynamicMethod(Division,typeof(void),new Type [] {typeof(Guid)} ,entityType.Module);

var il = DynamicMethod.GetILGenerator();

il.Emit(OpCodes.Ldarg_0);

il。 Emit(OpCodes.Callvirt,entityType.GetProperty(Id)。GetSetMethod());

il.Emit(OpCodes.Ret);

解决方案

如果你有这个:

 使用系统; 

命名空间 YourNameSpace
{
public abstract class PersonBase
{
public PersonBase(Guid id)
{
Id = id;
}

// 在ctor或ResetID方法之外隐藏/ span>
内部 Guid ID { private 设置; get ; }

内部 void ResetID(Guid id)
{
Id = id;
}
}

public class 人员: PersonBase
{
public Person(Guid id): base (id)
{
}

public Person(): base (Guid.Empty)
{
}
}
}

你可以这样做来使用'Activator.CreateInstance,并传递一个值'Guid参数:

 Person somePerson = Activator.CreateInstance( typeof (Person), new  对象 [] {Guid.NewGuid()}) as  Person;  ^ ] 


感谢重播。示例代码如下



  public  摘要 基础
{
public readonly Id;
}

public class 人:基数
{
}





我创建人物实例,如Activator.CreateInstance< t>()



i想要设置Id。有什么建议吗?



谢谢


Hi
I try to create dynamic method as

public void SetId(Guid id)
{
  Id = id;
}



I try but Id is private set

var DynamicMethod = new DynamicMethod("Division", typeof(void), new Type[] { typeof(Guid) }, entityType.Module);
var il = DynamicMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt, entityType.GetProperty("Id").GetSetMethod());
il.Emit(OpCodes.Ret);



how can i do this

What I have tried:

DynamicMethod = new DynamicMethod("Division", typeof(void), new Type[] { typeof(Guid) }, entityType.Module);
var il = DynamicMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt, entityType.GetProperty("Id").GetSetMethod());
il.Emit(OpCodes.Ret);

解决方案

If you have this:

using System;

namespace YourNameSpace
{
    public abstract class PersonBase
    {
        public PersonBase(Guid id)
        {
            Id = id;
        }

        // hide from exposure outside 'ctor or 'ResetID method
        internal Guid Id { private set; get; }

        internal void ResetID(Guid id)
        {
            Id = id;
        }
    }
 
    public class Person : PersonBase
    {
        public Person(Guid id) : base(id)
        {
        }

        public Person() : base(Guid.Empty)
        {
        }
    }
}

You can do this to use 'Activator.CreateInstance, and pass a value for the 'Guid parameter:

Person somePerson = Activator.CreateInstance(typeof (Person), new Object[]{Guid.NewGuid()}) as Person;

Hope that's helpful. If you are doing a lot fancy composition of .NET objects at run-time, I suggest you research the use of T4: [^]


Thanks for replay. Sample code is below

public abstract class Base
{
   public readonly Id;
}

public class Person : Base
{
}



I create Person Instance like Activator.CreateInstance<t>()

i want to set Id. Any suggestion ?

thanks


这篇关于何可以创建动态方法c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆