public static object CreateObject(Type type, params object[] args) { TkDebug.AssertArgumentNull(type, "type", null); //TkDebug.AssertEnumerableArgumentNull(args, "args", null); try { return(Activator.CreateInstance(type, args)); } //catch (MissingMethodException ex) //{ // TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture, // "没有找到类型{0}参数为{1}的构造函数,请确认", type, GetMethodSign(args)), ex, null); //} catch (TargetInvocationException ex) { TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture, "调用类型{0}参数为{1}的构造函数时,发生例外,请调试你的代码", type, GetMethodSign(args)), ex, null); } //catch (MethodAccessException ex) //{ // TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture, // "类型{0}参数为{1}的构造函数访问权限不够", // type, GetMethodSign(args)), ex, null); //} catch (MemberAccessException ex) { TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture, "类型{0}可能是一个抽象类型,无法创建", type), ex, null); } catch (Exception ex) { TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture, "调用类型{0}参数为{1}的构造函数是发生例外", type, GetMethodSign(args)), ex, null); } return(null); }
public static ITkTypeConverter GetConverter(PropertyInfo property, Type propertyType) { TkDebug.AssertArgumentNull(property, "property", null); TkDebug.AssertArgumentNull(propertyType, "propertyType", null); Attribute attr = Attribute.GetCustomAttribute(property, typeof(TkTypeConverterAttribute)); if (attr != null) { return(attr.Convert <TkTypeConverterAttribute>().CreateTypeConverter(propertyType)); } if (BaseGlobalVariable.Current != null) { ITkTypeConverter result = BaseGlobalVariable.Current.GetExTypeConverter(property); if (result != null) { return(result); } } return(null); }
public static string GetRealFileName(string fileName, FilePathPosition position) { TkDebug.ThrowIfNoAppSetting(); switch (position) { case FilePathPosition.Application: fileName = Path.Combine(BaseAppSetting.Current.AppPath, fileName); break; case FilePathPosition.Error: fileName = Path.Combine(BaseAppSetting.Current.ErrorPath, fileName); break; case FilePathPosition.Solution: fileName = Path.Combine(BaseAppSetting.Current.SolutionPath, fileName); break; case FilePathPosition.Xml: fileName = Path.Combine(BaseAppSetting.Current.XmlPath, fileName); break; } return(fileName); }
public static string LogException(ExceptionData logData) { if (logData == null) { return(null); } TkDebug.ThrowIfNoAppSetting(); if (!BaseAppSetting.Current.UseWorkThread) { return(null); } TkDebug.ThrowIfNoGlobalVariable(); string fileName = WebAppSetting.WebCurrent.GetExceptionLogName(logData.Exception); string content = logData.WriteXml(); Encoding encoding = BaseAppSetting.Current.WriteSettings.Encoding; BaseGlobalVariable.Current.BeginInvoke(new Action <string, string, Encoding>(SaveFile), fileName, content, encoding); return(fileName); }
/// <summary> /// 获得资源文件的ResourceManager /// </summary> /// <param name="assembly">程序集</param> /// <param name="partResName">资源文件的节点名称</param> /// <returns>资源文件的ResourceManager</returns> public static ResourceManager GetResourceManager(Assembly assembly, string partResName) { TkDebug.AssertArgumentNull(assembly, "assembly", null); TkDebug.AssertArgumentNullOrEmpty(partResName, "partResName", null); string[] resNames = assembly.GetManifestResourceNames(); string resName = null; foreach (string name in resNames) { if (name.IndexOf(partResName, StringComparison.Ordinal) != -1) { resName = name; break; } } if (resName == null) { return(null); } resName = resName.Substring(0, resName.Length - 10); // remove ".resources" return(new ResourceManager(resName, assembly)); }
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); } }
public static List <T> CreateListFromTable <T>(this DataTable table, string modelName, ReadSettings settings, Func <T> createFunc) { if (table == null) { return(null); } TkDebug.AssertArgumentNull(settings, "settings", null); TkDebug.AssertArgumentNull(createFunc, "createFunc", null); List <T> result = new List <T>(table.Rows.Count); IObjectSerializer serializer = ObjectExtension.CreateSerializer("DataRow"); foreach (DataRow row in table.Rows) { T obj = createFunc(); object reader = serializer.CreateCustomReader(row); serializer.ReadObject(reader, obj, modelName, settings, QName.Toolkit, null); result.Add(obj); } return(result); }
public void WriteObject(object writer, object receiver, string modelName, WriteSettings settings, QName root, object serializerData) { XmlWriter xmlWriter = writer.Convert <XmlWriter>(); var prefixTable = serializerData.Convert <PrefixTable>(); WriteStartElement(xmlWriter, root, prefixTable); ObjectInfo info = ObjectInfo.Create(receiver, modelName); if (info.IsObjectContext) { TkDebug.ThrowIfNoGlobalVariable(); BaseGlobalVariable.Current.ObjectContext.Push(receiver); } foreach (var attr in info.Attributes) { SerializerUtil.WritePropertyInfo(this, writer, receiver, settings, attr, prefixTable); } if (info.Content != null) { SerializerUtil.WritePropertyInfo(this, writer, receiver, settings, info.Content, prefixTable); } else { SerializerUtil.WriteChildElement(this, writer, info.Elements, settings, receiver, (objValue, propInfo) => propInfo.GetValue(objValue), prefixTable); } if (info.IsObjectContext) { BaseGlobalVariable.Current.ObjectContext.Pop(); } xmlWriter.WriteEndElement(); }
public object GetFactoryDefaultObject(string sectionName, string objectName) { TkDebug.AssertArgumentNullOrEmpty(sectionName, nameof(sectionName), this); TkDebug.AssertArgumentNullOrEmpty(objectName, nameof(objectName), this); var result = GetCustomObject(sectionName, objectName); if (result != null) { return(result); } var section = GetFactorySection(sectionName); if (section != null) { if (section.TryGetValue(objectName, out result)) { return(result); } } return(null); }
public static object GetValue(PropertyInfo info, object receiver) { TkDebug.AssertArgumentNull(info, "info", null); TkDebug.AssertArgumentNull(receiver, "receiver", null); try { return(info.GetValue(receiver, null)); } catch (MemberAccessException ex) { TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture, "无法从对象{0}中获取属性{1}的值,请检查get的访问权限", receiver.GetType(), info.Name), ex, null); } catch (Exception ex) { TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture, "对象{0}中可能不存在属性{1},请检查", receiver.GetType(), info.Name), ex, null); } TkDebug.ThrowImpossibleCode(null); return(null); }
public static DataTable CreateTable(this IEnumerable list, string tableName, string modelName, WriteSettings settings) { TkDebug.AssertArgumentNullOrEmpty(tableName, "tableName", null); TkDebug.AssertArgumentNull(settings, "settings", null); if (list == null) { return(null); } object value = list.Cast <object>().FirstOrDefault(); if (value == null) { return(null); } ObjectInfo info = ObjectInfo.Create(value, modelName); DataTable table = new DataTable(tableName) { Locale = ObjectUtil.SysCulture }; CreateTableStructure(info, table); IObjectSerializer serializer = ObjectExtension.CreateSerializer("DataRow"); foreach (var item in list) { DataRow row = table.NewRow(); object writer = serializer.CreateCustomWriter(row); serializer.WriteObject(writer, item, null, settings, QName.Toolkit, null); table.Rows.Add(row); } return(table); }
public void ReadComplexContent(ComplexContentAttribute attribute, object reader, object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData) { TkDebug.ThrowToolkitException("Json不支持ComplexContent节点", this); }
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); }
private static void AssertContent(object obj, Type configType, ObjectPropertyInfo content) { TkDebug.Assert(content == null, string.Format(ObjectUtil.SysCulture, "类型{0}只能配置一个Content特性,现在配置了不止一个", configType), obj); }
public bool Constains(AssemblyName name) { TkDebug.AssertArgumentNull(name, "name", this); return(fLoadedAssembly.Contains(name.FullName)); }
public static string Decrypt(string source) { TkDebug.ThrowIfNoAppSetting(); return(Decrypt(BaseAppSetting.Current.SecretKey, source)); }
public static byte[] Encrypt(byte[] source) { TkDebug.ThrowIfNoAppSetting(); return(Encrypt(BaseAppSetting.Current.SecretKey, source)); }
public EnumFieldValueAttribute(string value) { TkDebug.AssertArgumentNull(value, "value", null); Value = value; }
public bool IsShowMetaData(IQueryString queryString) { TkDebug.AssertArgumentNull(queryString, "queryString", this); return(queryString[fQueryStringName] == fMetaDataValue); }
public bool IsShowExcel(IQueryString queryString) { TkDebug.AssertArgumentNull(queryString, "queryString", this); return(queryString[fQueryStringName] == fExcelValue); }
public void AddXmlPlugInAttribute(XmlPlugInAttribute attribute) { TkDebug.AssertArgumentNull(attribute, "attribute", this); fXmlPlugInAttributes.Add(attribute); }
public DynamicElementAttribute(string factoryName) { TkDebug.AssertArgumentNullOrEmpty(factoryName, "factoryName", null); FactoryName = factoryName; }
public static QName Get(string localName, string @namespace) { TkDebug.AssertArgumentNullOrEmpty(localName, "localName", null); return(new QName(@namespace, localName)); }
protected internal void AddHost(string name, string value) { TkDebug.AssertArgumentNullOrEmpty(name, "name", this); fHosts[name] = value; }
public void WriteComplexContent(ComplexContentAttribute attribute, object writer, object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData) { TkDebug.ThrowToolkitException("Json不支持ComplexContent节点", this); }
internal static void AssertHttpContext() { TkDebug.AssertNotNull(HttpContextHelper.Current, "当前的HttpContext不存在,不能使用与Web相关的对象", null); }
private void InternalRead(JsonTextReader reader, object receiver, string modelName, ReadSettings settings) { ObjectInfo info = ObjectInfo.Create(receiver, modelName); if (info.IsObjectContext) { TkDebug.ThrowIfNoGlobalVariable(); BaseGlobalVariable.Current.ObjectContext.Push(receiver); } bool readElement = false; while (reader.Read()) { if (reader.TokenType == JsonToken.EndObject) { break; } AssertReadState(reader, JsonToken.PropertyName, null); string localName = reader.Value.Convert <string>(); ObjectPropertyInfo property = info.GetAttribute(localName); if (property != null) { property.Attribute.ReadObject(this, reader, receiver, settings, property, null); } else { property = info.Elements[localName]; if (property != null) { readElement = true; property.Attribute.ReadObject(this, reader, receiver, settings, property, null); } else { if (localName == CONTENT_NAME && info.Content != null) { property = info.Content; property.Attribute.ReadObject(this, reader, receiver, settings, property, null); } else { property = SerializerUtil.CustomRead(receiver, localName, modelName, () => null); if (property != null) { property.Attribute.ReadObject(this, reader, receiver, settings, property, null); } else { SkipProperty(reader); } } } } } SerializerUtil.ReadObjectCallBack(receiver); if (readElement) { SerializerUtil.CheckElementRequired(info.Elements, receiver); } if (info.IsObjectContext) { BaseGlobalVariable.Current.ObjectContext.Pop(); } }
public static QName Get(XmlReader reader) { TkDebug.AssertArgumentNull(reader, "reader", null); return(new QName(reader.NamespaceURI, reader.LocalName)); }
public static IEnumerable <T> Convert <T>(T value) { TkDebug.AssertArgumentNull(value, nameof(value), null); yield return(value); }
public ErrorPageExceptionHandler(IPageMaker pageMaker) { TkDebug.AssertArgumentNull(pageMaker, "pageMaker", null); fPageMaker = pageMaker; }