Ejemplo n.º 1
0
        internal static object InternalGetDefaultValue(Type type, string strValue, object defaultValue,
                                                       ReadSettings settings, bool throwException)
        {
            ITkTypeConverter converter = TkTypeDescriptor.GetConverter(type);

            if (throwException)
            {
                TkDebug.AssertNotNull(converter, string.Format(ObjectUtil.SysCulture,
                                                               "无法获取类型{0}的TypeConverter,请确认是否为其配置TypeConverterAttribute",
                                                               type), null);
            }
            else
            {
                if (converter == null)
                {
                    return(GetTypeDefaultValue(type));
                }
            }
            try
            {
                return(strValue == null?InternalGetDefaultValue(type, defaultValue)
                           : converter.ConvertFromString(strValue, settings));
            }
            catch
            {
                return(InternalGetDefaultValue(type, defaultValue));
            }
        }
Ejemplo n.º 2
0
        public static CustomObjectPropertyInfo CustomRead(object receiver, string localName,
                                                          string modelName, Func <string> versionFunc)
        {
            ICustomReader reader = receiver as ICustomReader;

            if (reader != null)
            {
                string version;
                if (reader.SupportVersion)
                {
                    TkDebug.AssertNotNull(versionFunc,
                                          "ICustomer的SupportVersion为true,但是versionFunc传入是null", receiver);
                    version = versionFunc();
                }
                else
                {
                    version = null;
                }
                var result = reader.CanRead(localName, version);
                if (result != null)
                {
                    var info = new CustomObjectPropertyInfo(localName, reader, result, version, modelName);
                    return(info);
                }
            }

            return(null);
        }
Ejemplo n.º 3
0
 internal static void AssertTypeConverter(object sender, Type type,
                                          ITkTypeConverter converter, string propertyName)
 {
     TkDebug.AssertNotNull(converter, string.Format(ObjectUtil.SysCulture,
                                                    "在分析属性{1}时出错:无法获取类型{0}的TypeConverter,请确认是否为其配置TkTypeConverterAttribute",
                                                    type, propertyName), sender);
 }
Ejemplo n.º 4
0
        public void ReadTagElement(TagElementAttribute attribute, object reader,
                                   object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            JsonTextReader jsonReader = reader.Convert <JsonTextReader>();

            AssertRead(jsonReader);
            AssertReadState(jsonReader, JsonToken.StartObject, info.PropertyName);
            while (jsonReader.Read())
            {
                if (jsonReader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                AssertReadState(jsonReader, JsonToken.PropertyName, info.PropertyName);
                string             localName = jsonReader.Value.ToString();
                ObjectPropertyInfo childInfo = attribute.ChildElements[localName];
                TkDebug.AssertNotNull(childInfo, string.Format(ObjectUtil.SysCulture,
                                                               "{0}没有在TagElement类型中声明,无法读取", localName), this);

                childInfo.Attribute.ReadObject(this, reader, receiver, settings, childInfo, serializerData);
            }

            SerializerUtil.CheckElementRequired(attribute.ChildElements, receiver);
        }
Ejemplo n.º 5
0
 private T OnInstanceCreated <T>(string regName, T result) where T : class
 {
     TkDebug.AssertNotNull(result, string.Format(ObjectUtil.SysCulture,
                                                 "无法创建{0}中注册名为{1}的插件", Name, regName), this);
     OnPlugInCreated(regName, result);
     CachePlugInObject(regName, result);
     return(result);
 }
Ejemplo n.º 6
0
        private BasePlugInFactory GetFactory(string name)
        {
            BasePlugInFactory factory = ObjectUtil.TryGetValue(fAllFactories, name);

            TkDebug.AssertNotNull(factory, string.Format(ObjectUtil.SysCulture,
                                                         "没有找到名称为{0}的代码工厂", name), this);
            return(factory);
        }
Ejemplo n.º 7
0
        public static T CreateFromXmlFactoryUseJson <T>(this string json, string factoryName, params object[] args)
        {
            IConfigCreator <T> obj = ReadJsonFromFactory <IConfigCreator <T> >(json, factoryName);

            TkDebug.AssertNotNull(obj, string.Format(ObjectUtil.SysCulture,
                                                     "\"{0}\"在配置工厂{1}中无法读出,请确认相关参数", json, factoryName), null);
            return(obj.CreateObject(args));
        }
Ejemplo n.º 8
0
        protected static string GetRegName(Type type)
        {
            EvaluateAdditionAttribute attr = Attribute.GetCustomAttribute(type,
                                                                          typeof(EvaluateAdditionAttribute)).Convert <EvaluateAdditionAttribute>();

            TkDebug.AssertNotNull(attr, string.Format(ObjectUtil.SysCulture,
                                                      "{0}类型没有附着EvaluateAdditionAttribute", type), type);
            return(attr.GetRegName(type));
        }
Ejemplo n.º 9
0
        public static T ConfirmQueryObject <T>(object owner, params object[] args) where T : class
        {
            T result = QueryObject <T>(args);

            TkDebug.AssertNotNull(result, string.Format(ObjectUtil.SysCulture,
                                                        "参数中没有包含类型为{0}的对象,请确认", typeof(T)), owner);

            return(result);
        }
Ejemplo n.º 10
0
        public Uri VerfiyGetHost(string name)
        {
            Uri result = GetHost(name);

            TkDebug.AssertNotNull(result, string.Format(ObjectUtil.SysCulture,
                                                        "没有在Application.xml中配置名称为{0}的Host", name), this);

            return(result);
        }
Ejemplo n.º 11
0
        public static T Convert <T>(this object objValue) where T : class
        {
            TkDebug.AssertArgumentNull(objValue, "objValue", null);

            T result = objValue as T;

            TkDebug.AssertNotNull(result, string.Format(ObjectUtil.SysCulture,
                                                        "将类型{0}转换为类型{1}失败,请确认代码", objValue.GetType(), typeof(T)), objValue);
            return(result);
        }
Ejemplo n.º 12
0
        public BasePlugInFactory GetCodeFactory(string factoryName)
        {
            TkDebug.AssertArgumentNullOrEmpty(factoryName, "factoryName", this);

            BasePlugInFactory result = ObjectUtil.TryGetValue(fCodeFactories, factoryName);

            TkDebug.AssertNotNull(result, string.Format(ObjectUtil.SysCulture,
                                                        "没有找到名称为{0}的代码插件工厂", factoryName), this);
            return(result);
        }
Ejemplo n.º 13
0
        public void Add(ObjectElementAttribute attr, PropertyInfo info)
        {
            TkDebug.AssertNotNullOrEmpty(attr.LocalName, string.Format(ObjectUtil.SysCulture,
                                                                       "属性{0}由于配置多个ObjectElement,因此需要指定LocalName", info.Name), attr);
            TkDebug.AssertNotNull(attr.ObjectType, string.Format(ObjectUtil.SysCulture,
                                                                 "属性{0}由于配置多个ObjectElement,因此需要指定LocalName", info.Name), attr);
            QName name = attr.GetQName(attr.LocalName);

            fXmlElements.Add(name, attr);
            fLocalElements.Add(name.LocalName, attr);
            fTypeElements.Add(attr.ObjectType, attr);
        }
Ejemplo n.º 14
0
 public void CheckRequired(Object receiver)
 {
     foreach (var entry in fElementWriter)
     {
         if (entry.Required)
         {
             //var propertyInfo = entry.ClassProperty;
             Object value = entry.GetValue(receiver); //ObjectUtil.GetValue(propertyInfo, receiver);
             TkDebug.AssertNotNull(value, string.Format(ObjectUtil.SysCulture,
                                                        "字段{0}设置了required=true,但是实际为null", entry.PropertyName), receiver);
         }
     }
 }
Ejemplo n.º 15
0
        public static Type GetRegType(string regName)
        {
            TkDebug.AssertArgumentNullOrEmpty(regName, "regName", null);

            TkDebug.ThrowIfNoGlobalVariable();
            RegTypeFactory factory = BaseGlobalVariable.Current.FactoryManager.
                                     GetCodeFactory(RegTypeFactory.REG_NAME).Convert <RegTypeFactory>();
            Type type = factory.GetType(regName);

            TkDebug.AssertNotNull(type, string.Format(ObjectUtil.SysCulture,
                                                      "没有找到注册名为{0}的注册类型", regName), null);

            return(type);
        }
Ejemplo n.º 16
0
        internal virtual BaseRegItem GetRegItem(string regName)
        {
            BaseRegItem result = ObjectUtil.TryGetValue(fCodePlugIns, regName);

            if (result == null)
            {
                RegItemEventArgs eventArgs = new RegItemEventArgs(regName);
                OnFailGetRegItem(eventArgs);
                result = eventArgs.RegItem;
            }

            TkDebug.AssertNotNull(result, string.Format(ObjectUtil.SysCulture,
                                                        "在{1}的插件工厂中没有找到注册名为{0}的插件", regName, Name), this);
            return(result);
        }
Ejemplo n.º 17
0
 public static object CreateObjectWithCtor(Type type)
 {
     TkDebug.AssertArgumentNull(type, "type", null);
     try
     {
         ConstructorInfo[] ctors = type.GetConstructors(FLAGS);
         var ctor = (from item in ctors
                     where item.GetParameters().Length == 0
                     select item).FirstOrDefault();
         TkDebug.AssertNotNull(ctor, string.Format(ObjectUtil.SysCulture,
                                                   "类型{0}没有配置不带参数的构造函数", type), type);
         return(ctor.Invoke(EMPTY));
     }
     catch (TargetInvocationException ex)
     {
         TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture,
                                                     "调用类型{0}的不带参数的构造函数时,发生例外,请调试你的代码", type), ex, null);
     }
     catch (MemberAccessException ex)
     {
         TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture,
                                                     "类型{0}可能是一个抽象类型,无法创建", type), ex, null);
     }
     catch (NotSupportedException ex)
     {
         TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture,
                                                     "类型{0}不支持创建", type), ex, null);
     }
     catch (SecurityException ex)
     {
         TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture,
                                                     "调用方不具有类型{0}访问权限", type), ex, null);
     }
     catch (Exception ex)
     {
         TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture,
                                                     "调用类型{0}的不带参数的构造函数是发生例外", type), ex, null);
     }
     return(null);
 }
Ejemplo n.º 18
0
        private static void AssertPlugInItemType(PropertyInfo property, Type xmlConfigType)
        {
            Type type = property.PropertyType;

            TkDebug.Assert(type.IsGenericType,
                           string.Format(ObjectUtil.SysCulture,
                                         "类型{1}的属性{0}必须是泛型类型", property.Name, xmlConfigType), null);
            Type[] gernericTypes = type.GetGenericArguments();
            TkDebug.Assert(gernericTypes != null && gernericTypes.Length == 1,
                           string.Format(ObjectUtil.SysCulture,
                                         "类型{1}的属性{0}的泛型参数只能是一个,现在可能没有或有多个",
                                         property.Name, xmlConfigType), null);
            Type assertType = gernericTypes[0];

            if (assertType != typeof(IXmlPlugInItem))
            {
                Type interfaceType = assertType.GetInterface(PlugInTypeName);
                TkDebug.AssertNotNull(interfaceType,
                                      string.Format(ObjectUtil.SysCulture,
                                                    "类型{1}的属性{0}的泛型参数必须实现IPlugInXmlItem接口,现在没有实现",
                                                    property.Name, xmlConfigType), null);
            }
        }
Ejemplo n.º 19
0
        public void Initialize(BaseAppSetting appSetting, object application)
        {
            TkDebug.AssertArgumentNull(appSetting, "appSetting", this);

            fManager = new AssemblyManager();
            if (NeitherContext)
            {
                AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
            }

            // 初始化AppPath下所有的Assembly
            AppPathAssembly = CreateAppPathAssembly(appSetting.AppPath, fManager);
            TkDebug.AssertNotNull(AppPathAssembly, "CreateAppPathAssembly返回为空", this);

            //fLoadedAssembly = AppPathAssembly.LoadedAssembly;
            // AppPath下的Assembly中查找添加PlugInFactory
            AppPathAssembly.AddPlugInFactory(FactoryManager);
            // 执行AppPath下的IInitialization
            var inits = AppPathAssembly.CreateInitializations();

            foreach (var item in inits)
            {
                try
                {
                    TkTrace.LogInfo($"执行{item.GetType()}的AppStarting");
                    item.AppStarting(application, appSetting, this);
                }
                catch (Exception ex)
                {
                    HandleStartedExeception("SAppStarting", item.GetType(), ex);
                }
            }

            // 搜索Code插件
            foreach (var assembly in AppPathAssembly)
            {
                if (assembly == ToolkitConst.TOOLKIT_CORE_ASSEMBLY)
                {
                    continue;
                }
                LoadAssembly(assembly);
            }

            fPlugIn = new PlugInAssembly(appSetting, fManager);
            //IEnumerable<AssemblyName> moduleAssembyNames = GetModuleAssemblies(appSetting);
            foreach (var assemblyName in fPlugIn)
            {
                if (!fManager.Contains(assemblyName.Name))
                {
                    var assembly = fPlugIn.LoadAssembly(fManager, NeitherContext, assemblyName);
                    //Assembly assembly = CreateAssembly(assemblyName);
                    //fLoadedAssemblyDict.Add(assembly.FullName, assembly);
                    if (assembly != null)
                    {
                        LoadAssembly(assembly);
                        //fLoadedAssembly.Add(assemblyName.FullName);
                    }
                }
            }

            if (NeitherContext)
            {
                AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
            }

            // 搜索Xml插件
            var factories = FactoryManager.CodeFactories;

            foreach (var factory in factories)
            {
                factory.SearchXmlPlugIn(appSetting.XmlPath);
            }

            ProcessConfigDefaultValue(appSetting);
            foreach (var item in inits)
            {
                try
                {
                    TkTrace.LogInfo($"执行{item.GetType()}的AppStarted");
                    item.AppStarted(application, appSetting, this);
                }
                catch (Exception ex)
                {
                    HandleStartedExeception("SAppStarted", item.GetType(), ex);
                }
            }

            LogPlugError(fPlugInErrorLog);
        }
Ejemplo n.º 20
0
 internal static void AssertHttpContext()
 {
     TkDebug.AssertNotNull(HttpContextHelper.Current,
                           "当前的HttpContext不存在,不能使用与Web相关的对象", null);
 }