关于C#反射的一些应用

本文探讨了C#反射的使用,特别是在字符串拼接生成动态脚本的场景中,如何利用反射实现代码的自动生成和编辑器扩展功能。
摘要由CSDN通过智能技术生成
下边分别是访问一个类的字段,属性,还有方法通过反射来获取对应的东西,下面就附上详细的代码

```csharp
    public void ReflectionClassGetFieldsInfoFunction<T>() where T : new()
    {
        T myInstance = new T();
        Type myType = typeof(T);
        try
        {
            
            FieldInfo[] myFields = myType.GetFields(BindingFlags.Public | BindingFlags.Instance);

            for(int i=0;i<myFields.Length;i++)
            {
                int index = i;
                if (myFields[index].GetValue(myInstance) is int)
                {
                    string name = myType.Name.ToLower() + "." + myFields[index].Name + "=" + ReturnIntProperty();
                    Debug.Log("FieldIntName:" + name);
                }
                else if(myFields[index].GetValue(myInstance) is bool)
                {
                    string name = myType.Name.ToLower() + "." + myFields[index].Name + "=" + ReturnTrueOrFalseProperty();
                    Debug.Log("FieldBoolName:" + name);
                }
                else if(myFields[index].GetValue(myInstance) is float)
                {
                    string name = myType.Name.ToLower() + "." + myFields[index].Name + "=" + ReturnFloatProperty();
                    Debug.Log("FieldfloatName:" + name);
                }

                else
                {
                    string name = myType.Name.ToLower() + "." + myFields[index].Name + "=" + "\"" + ReturnStringProperty() + "\"";
                    Debug.Log("Fieldsstringname:" + name);
                }
            }

        }
        catch(Exception e)
        {
            Debug.Log("捕获奇怪的异常");
        }

    }

    public void ReflectionClassGetPropertyInfoFunction<T>() where T:new()
    {
       

        T hello = new T();

        Type type = hello.GetType();

   
        PropertyInfo[] propertyInfo = type.GetProperties();

        Debug.Log("Class类:" + type.Name);
        
        Debug.Log("PropertyInfoCount:" + propertyInfo.Length);

      
        foreach (PropertyInfo Info in propertyInfo)
        {
           
            object value = Info.GetValue(hello);

            if (value is int)
            {
                string name =type.Name.ToLower()+"."+Info.Name + "=" + ReturnIntProperty();
                Debug.Log("intname:" + name);
            }

            else if (value is double)
            {
                string name = type.Name.ToLower()+"."+Info.Name + "=" + ReturnDoubleProperty();
                Debug.Log("doublename:" + name);
            }
            else if (value is float)
            {
                string name =type.Name.ToLower()+"."+ Info.Name + "=" + ReturnFloatProperty();
                Debug.Log("floatname:" + name);
            }
            else if(value is bool)
            {
                string name = type.Name.ToLower()+"."+Info.Name + "=" + ReturnTrueOrFalseProperty();
                Debug.Log("boolname:" + name);
            }

            else
            {
                string name = type.Name+"."+Info.Name + "=" + "\"" + ReturnStringProperty() + "\"";
                Debug.Log("stringname:" + name);
            }

        }
        
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值