如何获取类或属性的自定义特性(Attribute) 问题说明: 在ActiveRecord或者其他的ORM等代码中, 我们经常可以看到自定义特性(Attribute)的存在(如下面的代码所示) [PrimaryKey(PrimaryKeyType.Native, "PostId")] public int Id { ...... } 看似非常神秘的东西, 我们在什么场合需要使用它, 我们该如何使用它?自定义特性(Attribute)在一些需要声明
public class A { public int Property1 { get; set; } } static void Main(){ A aa = new A(); Type type = aa.GetType();//获取类型 System.Reflection.PropertyInfo propertyInfo = type.GetProperty("Property1"); propertyInfo.SetValue(aa, , null);//给对应属性赋值 in
public class A { public int Property1 { get; set; } } static void Main(){ A aa = new A(); Type type = aa.GetType();//获取类型 System.Reflection.PropertyInfo propertyInfo = type.GetProperty("Property1"); propertyInfo.SetValue(aa, 5, null);//给对应属性赋值 i
上一篇文章学习了IL的入门,接下来我们再通过两个例子来了解下类的属性.构造函数以及接口的使用 一.类的属性.构造函数 1.先看下我们要构建的类的C#代码,然后再进行IL的实现,示例代码如下: [Serializable] public class Dynamic { ; public const string ConstField = "const"; /// <summary> /// 定义属性 /// </summary> public int A { ge