zoukankan      html  css  js  c++  java
  • C#类型获取实例

    public static class ToObject<T> where T : class
    {
       public static Dictionary<string, string> values = new Dictionary<string, string>();
      
       static object target;
    
       private static void CreateInitiate()
    
      {
    
        Type t = typeof(T);
    
        ConstructorInfo ct = t.GetConstructor(System.Type.EmptyTypes);
    
        target = (T)ct.Invoke(null);
    
      }
    
    public static T ToEgg()
    {
    
    if (target != null)
    
    {
    
    target = null;
    
    }
    
    CreateInitiate();
    
    Type t = target.GetType();
    
    FieldInfo[] fields = t.GetFields();
    
    string fieldName = string.Empty;
    
    values.Add("id", "200");
    values.Add("path", "Model/Image");
    
    foreach (FieldInfo f in fields)
    
    {
    
    fieldName = f.Name;
    Debug.Log(fieldName);
    f.SetValue(target, Convert.ChangeType(values[fieldName], f.FieldType));
    
    }
    return (T)target;
    
    }
    
     
    
    
    }

    这样就可以经过修改,把Json字符串或者xml字符串转化为类了

  • 相关阅读:
    vue基础笔记
    HM.DAY-02
    前端基础(三)
    前端基础 (二)
    前端基础(一)
    爬虫基础入门(二)
    爬虫基础入门(一)
    Python进阶(三)
    Python进阶(二)
    python切片
  • 原文地址:https://www.cnblogs.com/ZhiXing-Blogs/p/7380695.html
Copyright © 2011-2022 走看看