栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

关于C#反射的一些应用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于C#反射的一些应用

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

```csharp
    public void ReflectionClassGetFieldsInfoFunction() 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() 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()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
            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;

    }

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/917812.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号