首页 > 代码库 > 动态创建对象的实现

动态创建对象的实现

public enum UpdateModelType : int    {        [RequestAttribute("eval_model_stat.json", typeof(ModelStatRequest))]        MODEL_STAT = 0,        /// <summary>        /// 财务历史 eval_fin_rpt_hyr.json        /// </summary>        [RequestAttribute("eval_fin_rpt_hyr.json", typeof(FinRptHyrRequest))]        FIN_RPT_HYR = 1,        /// <summary>        /// 资产负债表 eval_fin_bs.json        /// </summary>        [RequestAttribute("eval_fin_bs.json", typeof(FinBsRequest))]        FIN_BS = 2,        /// <summary>        /// 融资预测 eval_fin_fund.json        /// </summary>        [RequestAttribute("eval_fin_fund.json", typeof(FinFundRequest))]        FIN_FUND = 3,        /// <summary>        /// 财务分析 eval_fin_analysis.json        /// </summary>        [RequestAttribute("eval_fin_analysis.json", typeof(FinAnalysisRequest))]        FIN_ANALYSIS = 4,        /// <summary>        /// 杜邦分析 eval_fin_dupont.json        /// </summary>        [RequestAttribute("eval_fin_dupont.json", typeof(FinDupontRequest))]        FIN_DUPONT = 5,        /// <summary>        /// 绝对估值 eval_fin_absolute.json        /// </summary>        [RequestAttribute("eval_fin_absolute.json", typeof(FinAbsoluteRequest))]        FIN_ABSOLUTE = 6    }
public class RequestAttribute:Attribute    {        public string Config { get; set; }        public Type RequestBodyType { get; set; }        public RequestAttribute(string config,Type requestBodyType)        {            this.Config = config;            this.RequestBodyType = requestBodyType;        }    }

Config表示取值规则,RequestBodyType要创建的数据类型

private RequestAttribute GetRequestAttrubute(UpdateModelType type)        {            RequestAttribute result = null;            FieldInfo fieldInfo = type.GetType().GetField(type.ToString());            RequestAttribute[] requestAttributes = (RequestAttribute[])fieldInfo.GetCustomAttributes(typeof(RequestAttribute), false);            if (requestAttributes != null && requestAttributes.Length > 0)            {                result = requestAttributes[0];            }            return result;        }

private Dictionary<string, PropertyInfo> GetProperties(Type t)        {            if (cacheProperties.ContainsKey(t.FullName))            {                return cacheProperties[t.FullName];            }            Dictionary<string, PropertyInfo> dicResult = new Dictionary<string, PropertyInfo>();            var properties = t.GetProperties();            foreach (var pro in properties)            {                // 优先从JsonPropertyAttribute取得别名                var customAttrubutes = pro.GetCustomAttributes(typeof(JsonPropertyAttribute), false);                string key = string.Empty;                if (customAttrubutes.Length > 0)                {                    key = (customAttrubutes[0] as JsonPropertyAttribute).PropertyName;                }                else                {                    key = pro.Name;                }                dicResult.Add(key, pro);                // 如果是复合类型,则递归取得子属性                if (pro.PropertyType != typeof(string) && pro.PropertyType.BaseType != typeof(ValueType))                {                    var subDic = GetProperties(pro.PropertyType);                    foreach (var subKey in subDic.Keys)                    {                        dicResult.Add(key + "." + subKey, subDic[subKey]);                    }                }            }            if (dicResult.Count > 0)            {                cacheProperties.Add(t.FullName, dicResult);            }            return dicResult;        }

GetProperties方法:讲属性取出,例如:
class A
{
  public int No{get;set;}
public B B{get;set;}
}
class B
{
public int b{get;set;}
public string name{get;set;}
}

取出结果为:
No
B.b
B.name





待续....


 

 

 

 

 

 
RequestAttribute的json表示配置文件位置,
 

 

动态创建对象的实现