Beispiel #1
0
        /// <summary>
        /// 创建远程代理对象
        /// </summary>
        /// <typeparam name="T">远程对象类</typeparam>
        /// <param name="url">远程对象的URL地址</param>
        public static T CreateProxy <T>(string url)
        {
            //获取远程对象的类型
            Type type = typeof(T);

            return(ConvertHelper.ConvertTo <T>(CreateProxy(type, url)));
        }
Beispiel #2
0
        /// <summary>
        /// 获取构造函数的自定义特性
        /// </summary>
        /// <typeparam name="T">目标类型,比如要获取类Class1的构造函数自定义属性Attribute1,
        /// 则传入Class1</typeparam>
        /// <typeparam name="K">获取的自定义特性,比如Attribute1</typeparam>
        /// <param name="types">构造函数的数据类型列表,比如构造函数有两个参数:
        /// string param1,int param2 . 则传入typeof(string),typeof(int)</param>
        public static K GetConstructorAttribute <T, K>(params Type[] types)
        {
            try
            {
                //获取目标类型的反射入口点
                Type type = typeof(T);

                //获取构造函数参数
                Type[] tempTypes;
                //如果types为空,则赋值为Type.EmptyTypes
                if (types.Length == 0)
                {
                    tempTypes = Type.EmptyTypes;
                }
                else
                {
                    tempTypes = types;
                }

                //获取构造函数信息
                ConstructorInfo info = type.GetConstructor(tempTypes);

                //获取自定义特性
                return(ConvertHelper.ConvertTo <K>(Attribute.GetCustomAttribute(info, typeof(K))));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #3
0
        /// <summary>
        /// 获取枚举成员的名称
        /// </summary>
        /// <typeparam name="T">枚举名,比如Enum1</typeparam>
        /// <param name="member">枚举成员实例或成员值,
        /// 范例:Enum1枚举有两个成员A=0,B=1,则传入Enum1.A或0,获取成员名称"A"</param>
        public static string GetMemberName <T>(object member)
        {
            //转成基础类型的成员值
            Type   underlyingType = GetUnderlyingType(typeof(T));
            object memberValue    = ConvertHelper.ConvertTo(member, underlyingType);

            //获取枚举成员的名称
            return(Enum.GetName(typeof(T), memberValue));
        }
Beispiel #4
0
        /// <summary>
        /// 获取枚举成员的值
        /// </summary>
        /// <typeparam name="T">枚举名,比如Enum1</typeparam>
        /// <param name="memberName">枚举成员的常量名,
        /// 范例:Enum1枚举有两个成员A=0,B=1,则传入"A"获取0</param>
        public static object GetMemberValue <T>(string memberName)
        {
            //获取基础类型
            Type underlyingType = GetUnderlyingType(typeof(T));

            //获取枚举实例
            T instance = GetInstance <T>(memberName);

            //获取枚举成员的值
            return(ConvertHelper.ConvertTo(instance, underlyingType));
        }
Beispiel #5
0
        /// <summary>
        /// 将XML文档反序列化为指定类型的对象
        /// </summary>
        /// <param name="xml">XML实例文档</param>
        /// <param name="type">XmlSerializer可序列化的对象的类型</param>
        /// <returns>反序列化后的指定类型的对象</returns>
        /// <remarks>
        /// 调用示例:XmlDeserialize(xml,typeof(ClassA));
        /// </remarks>
        public static T DeserializeFromXml <T>(string xml)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));

#warning 待测试"Encoding.UTF8.GetBytes(xml)"有无问题
            //using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(xml)))
            {
                return(ConvertHelper.ConvertTo <T>(serializer.Deserialize(stream)));
            }
        }
Beispiel #6
0
        /// <summary>
        /// 获取属性的自定义特性
        /// </summary>
        /// <typeparam name="T">目标类型,比如要获取类Class1的构造函数自定义属性Attribute1,
        /// 则传入Class1</typeparam>
        /// <typeparam name="K">获取的自定义特性,比如Attribute1</typeparam>
        /// <param name="propertyName">字符串形式的属性名</param>
        public static K GetPropertyAttribute <T, K>(string propertyName)
        {
            try
            {
                //获取目标类型的反射入口点
                Type type = typeof(T);

                //获取构造函数信息
                PropertyInfo info = type.GetProperty(propertyName, BindingFlags.Public);

                //获取自定义特性
                return(ConvertHelper.ConvertTo <K>(Attribute.GetCustomAttribute(info, typeof(K))));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #7
0
        /// <summary>
        /// 将XML文件反序列化为对象
        /// </summary>
        /// <typeparam name="T">要获取的类</typeparam>
        /// <param name="xmlFile">Xml文件名,即保存序列化数据的位置</param>
        public static T DeSerializeFromXML <T>(string xmlFile)
        {
            try
            {
                //创建XML序列化对象
                XmlSerializer serializer = new XmlSerializer(typeof(T));

                //创建文件流
                using (FileStream fs = new FileStream(xmlFile, FileMode.Open))
                {
                    //开始反序列化对象
                    return(ConvertHelper.ConvertTo <T>(serializer.Deserialize(fs)));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #8
0
        /// <summary>
        /// 将Soap格式的XML文件反序列化为对象
        /// <remarks>
        /// 此序列化方式的效率相对较低,请优先使用二进制序列化与XML序列化方式处理
        /// </remarks>
        /// </summary>
        /// <typeparam name="T">要获取的类</typeparam>
        /// <param name="xmlFile">Xml文件名,即保存序列化数据的位置</param>
        public static T DeSerializeFromSoapXML <T>(string xmlFile)
        {
            try
            {
                //创建Soap序列化对象
                SoapFormatter serializer = new SoapFormatter();

                //创建文件流
                using (FileStream fs = new FileStream(xmlFile, FileMode.Open))
                {
                    //开始反序列化对象
                    return(ConvertHelper.ConvertTo <T>(serializer.Deserialize(fs)));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #9
0
        /// <summary>
        /// 将二进制文件反序列化为对象
        /// </summary>
        /// <typeparam name="T">要获取的类</typeparam>
        /// <param name="fileName">文件名,保存二进制序列化数据的位置</param>
        public static T DeSerializeFromBinary <T>(string fileName)
        {
            try
            {
                //创建二进制序列化对象
                BinaryFormatter serializer = new BinaryFormatter();

                //创建文件流
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                {
                    //开始反序列化对象
                    return(ConvertHelper.ConvertTo <T>(serializer.Deserialize(fs)));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #10
0
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <typeparam name="T">返回值类型</typeparam>
        /// <param name="instance">实例,在该实例上执行方法。</param>
        /// <param name="methodName">调用的方法名</param>
        /// <param name="flag">搜索标志</param>
        /// <param name="parameters">调用的参数</param>
        public static T InvokeMethod <T>(object instance, string methodName, BindingFlags flag, params object[] parameters)
        {
            //创建返回值
            object result;

            //获取类型
            Type type = instance.GetType();

            if (parameters.Length == 0)
            {
                result = type.InvokeMember(methodName, flag, null, instance, new object[] { });
            }
            else
            {
                result = type.InvokeMember(methodName, flag, null, instance, parameters);
            }

            //返回值
            return(ConvertHelper.ConvertTo <T>(result));
        }
Beispiel #11
0
 /// <summary>
 /// 创建类的实例
 /// </summary>
 /// <typeparam name="T">转换的目标类型</typeparam>
 /// <param name="className">类名,格式:"命名空间.类名"</param>
 /// <param name="parameters">传递给构造函数的参数</param>
 public static T CreateInstance <T>(string className, params object[] parameters)
 {
     return(ConvertHelper.ConvertTo <T>(CreateInstance(className, parameters)));
 }
Beispiel #12
0
 /// <summary>
 /// 创建类的实例
 /// </summary>
 /// <typeparam name="T">转换的目标类型</typeparam>
 /// <param name="type">类型</param>
 /// <param name="parameters">传递给构造函数的参数</param>
 public static T CreateInstance <T>(Type type, params object[] parameters)
 {
     return(ConvertHelper.ConvertTo <T>(CreateInstance(type, parameters)));
 }
Beispiel #13
0
 /// <summary>
 /// 将字节数组反序列化为对象
 /// </summary>
 /// <typeparam name="T">要获取的类</typeparam>
 /// <param name="buffer">要反序列化的字节数组</param>
 public static T DeSerializeFromBytes <T>(byte[] buffer)
 {
     return(ConvertHelper.ConvertTo <T>(DeSerializeFromBytes(buffer)));
 }