using System; using System.Reflection; namespace attributeTest { [System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct)] public class AuthorAttribute : System.Attribute { public string name { get; set; } public double verson { get; set; } public AuthorAttribute(string nm, double vr) { this.name = nm; this.verson = vr; } } [Author("xiaozhang", 2.0)] public class A { private double a; public B b { get; set; } public double getdoubleina() { return a; } public A(double a) { this.a = a; b = new B("xiaozhang"); } } public class B { public string Name { get; set; } public B(string n) { Name = n; } } class Program { static void Main(string[] args) { A a21
C# 反射获得字段 和 修改字段的方法
最新推荐文章于 2024-08-16 16:09:49 发布
本文演示了如何使用C#的反射来获取和修改类的字段及属性值。通过`GetProperties()`获取属性,使用`GetValue()`和`SetValue()`方法读写属性值。同时展示了获取和修改私有字段值的示例,以及如何利用`GetCustomAttributes()`获取类上的自定义特性。
摘要由CSDN通过智能技术生成