何可以创建动态方法c#

编程入门 行业动态 更新时间:2024-10-11 13:28:12
本文介绍了何可以创建动态方法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#

本文发布于:2023-11-12 09:56:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581171.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   动态   何可

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!