码迷,mamicode.com
首页 > Windows程序 > 详细

C#反射读取和设置类的属性

时间:2015-02-03 19:15:26      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

C#反射技术的简单操作(读取和设置类的属性)

http://www.cnblogs.com/william-lin/archive/2013/06/05/3118233.html

泛型方法通过反射创建类的实例

 /// <summary>
        /// 获取web服务实例
        /// </summary>
        /// <typeparam name="T">服务代理类</typeparam>
        /// <returns></returns>
        public static T GetInsatnce<T>() where T : System.Web.Services.Protocols.SoapHttpClientProtocol
        {
            string fullWebUrlPath = string.Empty;
            Type type = typeof(T);
            object obj = Activator.CreateInstance(type);
            T instance = obj as T;
            System.Reflection.PropertyInfo propertyInfo = type.GetProperty("Url");
            fullWebUrlPath = Prefix_WebServiceURL + string.Format("/{0}/{0}.asmx", type.Name);
            propertyInfo.SetValue(obj, fullWebUrlPath, null);
            return instance;
        }

 调用:

 var loginService = ServiceProxyFactory.GetInsatnce<Globalegrow.ServiceProxy.LoginServiceProxy.LoginService>();

 

C#反射读取和设置类的属性

标签:

原文地址:http://www.cnblogs.com/zfanlong1314/p/4270483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!