Пример #1
0
        /// <summary>
        /// 直接实例化实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private T CreateInstance <T>() where T : class
        {
            MemberInfo         info = typeof(T);
            ImplementAttribute atri = info.GetCustomAttribute(typeof(ImplementAttribute)) as ImplementAttribute;

            return((T)Activator.CreateInstance(atri.ImplementClass));
        }
Пример #2
0
 /// <summary>
 /// 从静态变量中寻找并实例化
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 private T CreateInstanceFromStaticProp <T>() where T : class
 {
     FieldInfo[] props = extendClass.GetFields(BindingFlags.Static | BindingFlags.NonPublic);
     foreach (FieldInfo p in props)
     {
         foreach (Type ip in p.FieldType.GetInterfaces())
         {
             if (ip.Name == typeof(T).Name)
             {
                 if (p.GetValue(p) == null)
                 {
                     MemberInfo         info = typeof(T);
                     ImplementAttribute atri = info.GetCustomAttribute(typeof(ImplementAttribute)) as ImplementAttribute;
                     p.SetValue(p, Activator.CreateInstance(atri.ImplementClass));
                 }
                 return(p.GetValue(p) as T);
             }
         }
     }
     return(null);
 }