C# 反射获得字段 和 修改字段的方法

本文演示了如何使用C#的反射来获取和修改类的字段及属性值。通过`GetProperties()`获取属性,使用`GetValue()`和`SetValue()`方法读写属性值。同时展示了获取和修改私有字段值的示例,以及如何利用`GetCustomAttributes()`获取类上的自定义特性。
摘要由CSDN通过智能技术生成
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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值