[个人笔记]C#反射

public static void Main() {
    User user = new User();
    Type a = typeof(User);
    Type usertype = Type.GetType("com.atguigu.fate.lin.User");
    Type t1 = user.GetType();
    /**
     * public 权限的属性
     */
    PropertyInfo[] properties = t1.GetProperties();
    foreach (PropertyInfo item in properties) {
        Console.WriteLine(item.Name);
    }
    Console.WriteLine("-----------------------------");
    /**
     * public 权限的成员变量
     */
    FieldInfo[] fields = t1.GetFields();
    foreach (FieldInfo item in fields) {
        Console.WriteLine(item.Name);
    }
    Console.WriteLine("-----------------------------");
    /**
     * public 权限的属性
     */
    MethodInfo[] methods = t1.GetMethods();
    foreach (MethodInfo item in methods) {
        Console.WriteLine(item.Name);
    }
    Console.WriteLine("-----------------------------");
    ConstructorInfo[] constructors = t1.GetConstructors();
    foreach (ConstructorInfo item in constructors) {
        Console.WriteLine(item.Name);
    }
    Console.WriteLine("-----------------------------");
    User user1 = (User)Activator.CreateInstance(t1);
    User user2 = Activator.CreateInstance<User>();

    PropertyInfo idproperty=t1.GetProperty("Id");
    idproperty.SetValue(user2, 1);
    FieldInfo[] fields1 = t1.GetFields(BindingFlags.Instance|BindingFlags.NonPublic);
    PropertyInfo nameproperty = t1.GetProperty("Name");
    nameproperty.SetValue(user1,"--");
    object name = nameproperty.GetValue(user2);
    
    Assembly util = Assembly.LoadFile(@"D:\BaiduNetdiskDownload\2020-11-20\WinReflectionAndDelegate\DbUtility\bin\Debug\DbUtility.dll");
     
    Assembly util = Assembly.Load("DbUtility");
    Assembly util = Assembly.LoadFrom("DbUtility.dll");
    Type help = util.GetType("DbUtility.SqlHelper");
    object x1 = util.CreateInstance("DbUtility.SqlHelper");
    MethodInfo method = help.GetMethod("ExecuteNonQuery",new Type[]{typeof(string),typeof(int),typeof(SqlParameter[]) });
    method.Invoke(null,new object[]{ "INSERT INTO UserX VALUES (3,'liusu2');" ,1,null});
    ConstructorInfo constructor = t1.GetConstructor(new Type[]{typeof(int)});
    object x2 = constructor.Invoke(new object[]{233});
    Console.WriteLine(x2);
}
delegate int Add(int n1,int n2);
delegate void Show(string msg);
public class Program {
    private void ShowMsg(string msg){
        Console.WriteLine(msg);
    }
    private void ShowInfo(string msg) {
        Console.WriteLine(msg);
    }
    private void ShowMsgNew(string msg) {
        Console.WriteLine(msg);
    }
    private void ShowMsgNew2(string msg) {
        Console.WriteLine(msg);
    }
    private int AddNums(int n1, int n2){
        return n1 + n2;
    }
    private int Add(int n1, int n2) {
        return n1 + n2;
    }
    public static void Main() {
        Program p = new Program();
        p.Test();
    }
    public void Test(){
        Add add = new Add(AddNums);
        int x1 = add.Invoke(1,1);
        int x2 = add(1, 1);
        Add add1 = AddNums;
        int x4 =add1(5, 7);
        int x3 = Add(4, 6);

        Show show = ShowMsg;
        show += ShowInfo;
        show += ShowMsgNew;
        show += ShowMsgNew2;
        show -= ShowMsgNew2;
        show("唯我超电磁炮永世长存!");

        Action a1 = () => {
            Console.WriteLine("----");
        };
        Action<int> a3 = a => {
            int x = a;
            int y = a + x + 12;
            Console.WriteLine("----");
        };
        Action<int,string,decimal> a2 = (a,b,c) => {

        };
        Func<int> x5 = () => {
            return 0;
        };
        Add x6 = delegate (int x, int y) {
            return x + y;
        };
        Func<string,int> x7 = (x) => {
            return 'x';
        };
    }
    public event Action AddHandler;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值