Exemple #1
0
        public static WebResponse HttpGet(Uri uri, string contentType)
        {
            TkDebug.AssertArgumentNull(uri, "uri", null);

            WebRequest request = WebRequest.Create(uri);

            TkTrace.LogInfo(uri.ToString());

            try
            {
                WebResponse response = request.GetResponse();
                if (!string.IsNullOrEmpty(contentType))
                {
                    TkDebug.Assert(response.ContentType.Contains(contentType), string.Format(
                                       ObjectUtil.SysCulture, "{0}返回的ContentType是{1},而目标是{2},两者不兼容",
                                       uri, response.ContentType, contentType), null);
                }
                return(response);
            }
            catch (WebException ex)
            {
                TkDebug.ThrowToolkitException(string.Format(
                                                  ObjectUtil.SysCulture, "访问{0}时出错", uri), ex, null);
                return(null);
            }
        }
Exemple #2
0
 protected void AssertCreateType <T>(Type RegType)
 {
     TkDebug.Assert(ObjectUtil.IsSubType(typeof(T), RegType),
                    string.Format(ObjectUtil.SysCulture,
                                  "注册类型{0}不是参数T类型{1}的子类,无法进行类型转换,请确认",
                                  RegType, typeof(T)), this);
 }
        public AssemblyPlugInFactoryAttribute(Type plugInFactoryType)
        {
            TkDebug.AssertArgumentNull(plugInFactoryType, "plugInFactoryType", null);

            PlugInFactoryType = plugInFactoryType;
            TkDebug.Assert(plugInFactoryType.IsSubclassOf(typeof(BasePlugInFactory)),
                           string.Format(ObjectUtil.SysCulture,
                                         "对象{0}必须从BasePlugInFactory继承,现在不是", plugInFactoryType), null);
        }
Exemple #4
0
        /// <summary>
        /// 获取枚举类型的第一个值
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static object GetFirstEnumValue(Type enumType)
        {
            TkDebug.AssertArgumentNull(enumType, "enumType", null);

            Array values = Enum.GetValues(enumType);

            TkDebug.Assert(values.Length > 0, string.Format(ObjectUtil.SysCulture,
                                                            "枚举类型{0}中没有枚举值", enumType), null);
            return((values as IList)[0]);
        }
Exemple #5
0
 internal static string GetXmlFilePattern(string pattern)
 {
     if (string.IsNullOrEmpty(pattern))
     {
         return("*.xml");
     }
     TkDebug.Assert(pattern.EndsWith(".xml", StringComparison.OrdinalIgnoreCase),
                    string.Format(ObjectUtil.SysCulture, "{0}应该是以.xml结束,而现在不是", pattern), null);
     return(pattern);
 }
Exemple #6
0
        public static T ParseEnum <T>(string value, bool ignoreCase) where T : struct
        {
            TkDebug.AssertArgumentNullOrEmpty(value, "value", null);

            Type enumType = typeof(T);

            TkDebug.Assert(enumType.IsEnum, string.Format(ObjectUtil.SysCulture,
                                                          "参数T的类型不是枚举类型,而是{0}", enumType), null);
            return((T)Enum.Parse(enumType, value, ignoreCase));
        }
Exemple #7
0
 private void AssertXmlPlugIn()
 {
     //fXmlPlugInAttributes.Sort(XmlPlugInAttributeComparer.Comparer);
     TkDebug.Assert(fBaseClassTypes.Count > 0, string.Format(ObjectUtil.SysCulture,
                                                             "类型{0}既然附着了XmlPlugInAttribute,就必须至少附着一个PlugInBaseClassAttribute以指明通过配置生成的类型",
                                                             GetType()), this);
     if (fDefaultBaseClassType == null)
     {
         fDefaultBaseClassType = fBaseClassTypes.First().Value;
     }
 }
Exemple #8
0
        internal static Type GetListValueType(Type propertyType, string name, object sender)
        {
            Type type = propertyType;

            TkDebug.Assert(type.IsGenericType, string.Format(ObjectUtil.SysCulture,
                                                             "无法从属性{0}的类型中获取ObjectType,因为其不是泛型类型", name), sender);
            Type[] args = type.GetGenericArguments();
            TkDebug.Assert(args.Length == 1, string.Format(ObjectUtil.SysCulture,
                                                           "类型{0}的泛型参数个数不为1,无法获取第一个泛型参数", type), sender);
            return(args[0]);
        }
        public bool Add(BasePlugInAttribute attribute, Type type)
        {
            TkDebug.AssertArgumentNull(attribute, "attribute", this);
            TkDebug.AssertArgumentNull(type, "type", this);

            TkDebug.Assert(attribute.FactoryName == Name, string.Format(ObjectUtil.SysCulture,
                                                                        "调用错误,Attribute的工厂名{0}和当前工厂的不一致", attribute.FactoryName), this);

            string regName = attribute.GetRegName(type);

            return(Add(regName, attribute, type));
        }
Exemple #10
0
        public CustomPropertyInfo(Type dataType, NamedAttribute attribute)
        {
            TkDebug.AssertArgumentNull(dataType, "dataType", null);
            TkDebug.AssertArgumentNull(attribute, "attribute", null);

            TkDebug.Assert(attribute is SimpleElementAttribute || attribute is BaseDictionaryAttribute,
                           string.Format(ObjectUtil.SysCulture,
                                         "attribute必须是ElementAttribute或者DictionaryAttribute,现在的attribute是{0}",
                                         attribute.GetType()), null);

            DataType  = dataType;
            Attribute = attribute;
        }
        public void ReadComplexContent(ComplexContentAttribute attribute, object reader,
                                       object receiver, ReadSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            XmlReader xml = reader.Convert <XmlReader>();

            TkDebug.Assert(xml.NodeType == XmlNodeType.Element, string.Format(ObjectUtil.SysCulture,
                                                                              "ComplexContent初始要求XmlReader在Element节点上,当前状态是{0},位置不对", xml.NodeType), this);
            QName  name  = QName.Get(xml);
            string value = ReadComplexContent(xml, name);

            SerializerUtil.CheckRequiredContent(attribute, receiver, info, value);
            SerializerUtil.SetObjectValue(receiver, settings, info, value, false);
        }
Exemple #12
0
        //public IConfigCreator<IExceptionHandler> ExceptionHandler { get; private set; }

        //public IConfigCreator<IExceptionHandler> ReLogOnHandler { get; private set; }

        //public IConfigCreator<IExceptionHandler> ErrorPageHandler { get; private set; }

        //public IConfigCreator<IExceptionHandler> ToolkitHandler { get; private set; }

        //public IConfigCreator<IExceptionHandler> ErrorOpeartionHandler { get; private set; }

        private void ConfigDatabase(WebAppXml xml)
        {
            if (xml.Databases != null)
            {
                bool entity = false;
                foreach (var item in xml.Databases)
                {
                    if (item.Default)
                    {
                        TkDebug.Assert(fDefault == null, string.Format(ObjectUtil.SysCulture,
                                                                       "{0}的Default属性标识为True,而前面已经有配置Default为True了,配置错误", item.Name), this);
                        fDefault = item;
                    }
                    if (!string.IsNullOrEmpty(item.ProviderName))
                    {
                        entity = true;
                    }
                    fDictionary.Add(item.Name, item);
                }
                if (fDefault == null && xml.Databases.Count > 0)
                {
                    fDefault = xml.Databases[0];
                }

                if (entity)
                {
                    //Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpRuntime.AppDomainAppVirtualPath);
                    //var connectionStrings = config.ConnectionStrings.ConnectionStrings;
                    //bool isModify = false;
                    //var entities = from item in xml.Databases
                    //               where !string.IsNullOrEmpty(item.ProviderName)
                    //               select item;
                    //foreach (var item in entities)
                    //{
                    //    ConnectionStringSettings setting = new ConnectionStringSettings(item.Name,
                    //        item.ConnectionString, item.ProviderName);
                    //    int index = connectionStrings.IndexOf(setting);
                    //    if (index == -1)
                    //    {
                    //        isModify = true;
                    //        connectionStrings.Remove(setting.Name);
                    //        connectionStrings.Add(setting);
                    //    }
                    //}
                    //if (isModify)
                    //    config.Save(ConfigurationSaveMode.Modified);
                }
            }
        }
Exemple #13
0
        public void AddBaseClassAttribute(XmlBaseClassAttribute attribute)
        {
            TkDebug.AssertArgumentNull(attribute, "attribute", this);

            if (!fBaseClassTypes.ContainsKey(attribute.BaseClass))
            {
                fBaseClassTypes.Add(attribute.BaseClass, attribute.RegType);
                if (attribute.Default)
                {
                    TkDebug.Assert(fDefaultBaseClassType == null, string.Format(ObjectUtil.SysCulture,
                                                                                "XmlBaseClassAttribute只能有一个属性声明为Default,在声明{0}之前已经有属性设置了Default=true了,请确认",
                                                                                attribute.BaseClass), this);
                    fDefaultBaseClassType = attribute.RegType;
                }
            }
        }
Exemple #14
0
        internal static List <XmlPlugInItemInfo> SearchConfigItem(Type xmlConfigType)
        {
            PropertyInfo[] properties = xmlConfigType.GetProperties(BindingFlags.Public
                                                                    | BindingFlags.NonPublic | BindingFlags.Instance);

            var configProperties = from memberInfo in properties
                                   from attribute in memberInfo.GetCustomAttributes(false)
                                   where attribute is XmlPlugInItemAttribute
                                   select memberInfo;
            List <XmlPlugInItemInfo> fConfigItems = new List <XmlPlugInItemInfo>();

            foreach (PropertyInfo propertyInfo in configProperties)
            {
                XmlPlugInItemType type           = XmlPlugInItemType.None;
                Type[]            interfaceTypes = propertyInfo.PropertyType.GetInterfaces();
                foreach (Type item in interfaceTypes)
                {
                    if (item == typeof(IEnumerable))
                    {
                        AssertPlugInItemType(propertyInfo, xmlConfigType);
                        type = XmlPlugInItemType.Enumerable;
                        break;
                    }
                    if (item == typeof(IEnumerator))
                    {
                        AssertPlugInItemType(propertyInfo, xmlConfigType);
                        type = XmlPlugInItemType.Enumerator;
                        break;
                    }
                    if (item == PlugInType)
                    {
                        type = XmlPlugInItemType.Single;
                        break;
                    }
                }
                TkDebug.Assert(type != XmlPlugInItemType.None, string.Format(ObjectUtil.SysCulture,
                                                                             "类型{1}的属性{0}虽然加注XmlPlugInItemAttribute的特性,但是它必须是IPlugInXmlItem"
                                                                             + "或者IEnumerable<IPlugInXmlItem>或者IEnumerator<IPlugInXmlItem>类型",
                                                                             propertyInfo.Name, xmlConfigType), null);
                fConfigItems.Add(new XmlPlugInItemInfo {
                    PropertyInfo = propertyInfo, Type = type
                });
            }
            TkDebug.Assert(fConfigItems.Count > 0, string.Format(ObjectUtil.SysCulture,
                                                                 "类型{0}没有一个属性加注XmlPlugInItemAttribute特性,这样无法读取配置", xmlConfigType), null);
            return(fConfigItems);
        }
Exemple #15
0
        /// <summary>
        /// 将字符串中的指定字符替换成转义字符串,算法效率相对高效
        /// </summary>
        /// <param name="source">需要转义的字符串</param>
        /// <param name="replaceString">需要替换字符串数组</param>
        /// <param name="escapeFunc">判断是否需要转义字符,返回-1无需转义,
        /// 返回其他数字将去数组replaceString查找需要替换的字符串</param>
        /// <returns></returns>
        public static string EscapeString(string source, string[] replaceString, Func <char, int> escapeFunc)
        {
            if (string.IsNullOrEmpty(source))
            {
                return(string.Empty);
            }
            TkDebug.AssertArgumentNull(escapeFunc, "escapeFunc", null);
            TkDebug.AssertEnumerableArgumentNull <string>(replaceString, "replaceString", null);

            StringBuilder cachedStringBuilder = null;
            int           start = 0;
            int           pos   = 0;
            int           count = source.Length;

            for (int i = 0; i < count; ++i)
            {
                pos = escapeFunc(source[i]);
                if (pos < 0)
                {
                    continue;
                }
                if (cachedStringBuilder == null)
                {
                    cachedStringBuilder = new StringBuilder();
                }
                cachedStringBuilder.Append(source.Substring(start, i - start));
                TkDebug.Assert(pos < replaceString.Length, string.Format(ObjectUtil.SysCulture,
                                                                         "参数escapeFunc返回的值有误,replaceString的长度为{0},但是返回值却是{1},越界了",
                                                                         replaceString.Length, pos), null);
                cachedStringBuilder.Append(replaceString[pos]);
                start = i + 1;
            }
            if (start == 0)
            {
                return(source);
            }
            else if (start < count)
            {
                cachedStringBuilder.Append(source.Substring(start, count - start));
            }
            string s = cachedStringBuilder.ToString();

            return(s);
        }
        public void WriteTagElement(TagElementAttribute attribute, object writer,
                                    object value, WriteSettings settings, ObjectPropertyInfo info, object serializerData)
        {
            DictionaryBuilder builder = writer.Convert <DictionaryBuilder>();
            QName             name    = info.QName;

            var infoArray = attribute.ChildElements.CreateOrderPropertyInfoList().ToArray();

            TkDebug.Assert(infoArray.Length == 1, string.Format(ObjectUtil.SysCulture,
                                                                "TagElement的子节点必须有且仅有一个,当前{1}下有{0}个", infoArray.Length, name), this);
            DictionaryBuilder subBuilder = builder.CreateBuilder();

            var item = infoArray[0];

            if (item.IsSingle)
            {
                item.Content.Attribute.WriteObject(this, subBuilder, value,
                                                   settings, item.Content, serializerData);
                builder.Add(name.LocalName, subBuilder);
            }
            else
            {
                IList list = value as IList;
                if (list != null)
                {
                    if (list.Count > 0)
                    {
                        var property = item.Get(list[0].GetType());
                        property.Attribute.WriteObject(this, subBuilder, value, settings,
                                                       property, serializerData);
                        builder.Add(name.LocalName, subBuilder);
                    }
                }
                else
                {
                    var property = item.Get(value.GetType());
                    property.Attribute.WriteObject(this, subBuilder, value, settings,
                                                   property, serializerData);
                    builder.Add(name.LocalName, subBuilder);
                }
            }
        }
Exemple #17
0
        public static IParameter Create(IEnumerable <string> names, IEnumerable <string> values)
        {
            TkDebug.AssertArgumentNull(names, "names", null);
            TkDebug.AssertArgumentNull(values, "values", null);

            string[] nameArray  = names.ToArray();
            string[] valueArray = values.ToArray();
            TkDebug.Assert(nameArray.Length == valueArray.Length, string.Format(ObjectUtil.SysCulture,
                                                                                "参数names中的个数和参数values中的个数不匹配,names有{0}个,而values有{1}个",
                                                                                nameArray.Length, valueArray.Length), null);

            Dictionary <string, string> dictionary = new Dictionary <string, string>();

            for (int i = 0; i < nameArray.Length; i++)
            {
                dictionary[nameArray[i]] = valueArray[i];
            }

            return(Create(dictionary));
        }
Exemple #18
0
        public void Add(BasePlugInFactory factory)
        {
            TkDebug.AssertArgumentNull(factory, "factory", this);

            lock (this)
            {
                TkDebug.Assert(!fAllFactories.ContainsKey(factory.Name), string.Format(ObjectUtil.SysCulture,
                                                                                       "{0}已经注册,请检查代码", factory.Name), factory);

                BaseXmlConfigFactory config = factory as BaseXmlConfigFactory;
                if (config != null)
                {
                    fXmlConfigs.Add(config.Name, config);
                }
                else
                {
                    fCodeFactories.Add(factory.Name, factory);
                }
                fAllFactories.Add(factory.Name, factory);
            }
        }
Exemple #19
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);
            }
        }
Exemple #20
0
 public static void CheckSimpleDictionary(BaseDictionaryAttribute attribute, object sender)
 {
     TkDebug.Assert(attribute is DictionaryAttribute, string.Format(ObjectUtil.SysCulture,
                                                                    "{0}仅支持DictionaryAttribute,即Value值为简单对象", sender), sender);
 }
Exemple #21
0
 private static void AssertContent(object obj, Type configType, ObjectPropertyInfo content)
 {
     TkDebug.Assert(content == null, string.Format(ObjectUtil.SysCulture,
                                                   "类型{0}只能配置一个Content特性,现在配置了不止一个",
                                                   configType), obj);
 }