关于C#反射的一些应用

下边分别是访问一个类的字段,属性,还有方法通过反射来获取对应的东西,下面就附上详细的代码

```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);
            }

        }
        
    }

    public void ReflectionClassGetMethodInfoFunction<T>()where T:new()
    {
        T hello = new T();

        Type type = hello.GetType();

        MethodInfo[] methodInfo = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

        foreach(MethodInfo temp in methodInfo)
        {
            if (!temp.Name.Contains("set_")&&!temp.Name.Contains("get_") )
            {
                Debug.Log("tempReturnType:" + temp.ReturnType);
                string name = type.Name + "." + temp.Name + "( );";
                Debug.Log("MethodName:" + name);
            }
        }

    }
public string ReturnStringProperty()
{
    
    
        int length = UnityEngine.Random.Range(5, 10);

        string result = " ";
        char[] pattern = new char[] {
    
     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
        };
        int n = pattern.Length;
        System.Random random = new System.Random(~unchecked((int)DateTime.Now.Ticks));
        for(int i=0;i<length;i++)
        {
    
    
            int rnd = random.Next(0, n);
            result += pattern[rnd];
        }

        return result;
}

public string ReturnIntProperty()
{
    
    
   string result =" ";
        int n = UnityEngine.Random.Range(0, 1000);

        result = n.ToString();
        return result;
}

public string ReturnFloatProperty()
{
    
    
        string result = " ";
        double n = UnityEngine.Random.Range(0.0f, 1000.0f);
        result = n.ToString();
        return result;
}

public string ReturnDoubleProperty()
{
    
    
        string result = " ";
        float n = UnityEngine.Random.Range(0.0f, 1000.0f);
        result = n.ToString();
        return result;

}

    public string ReturnTrueOrFalseProperty()
    {
    
    
        string result = " ";
        int tempindex = UnityEngine.Random.Range(0, 2);
        if (tempindex == 0)
            result = "true";
        else
            result = "false";
        return result;

    }

因为我是要用到字符串拼接娜塔莉变得内容让后通过字符串拼接成脚本到时候用来自动生成代码

猜你喜欢

转载自blog.csdn.net/charlsdm/article/details/124887513
今日推荐