private static IPetShopCacheDependency LoadInstance(string className) { string path = ConfigurationManager.AppSettings["CacheDependencyAssembly"]; string fullyQualifiedClass = path + "." + className; // Using the evidence given in the config file load the appropriate assembly and class return (IPetShopCacheDependency)Assembly.Load(path).CreateInstance(fullyQualifiedClass); }
个人爱好,在csdn上看到自己觉得有收藏价值的,就整理一下放到了自己的blog里,下次查起来方便啊
反射应用之一:根据控件名、属性名进行取值和赋值 '必须引用命名空间System.Reflection,System.ComponentModel '以下根据控件名和属性名取值 Public Function GetValueControlProperty(ByVal ClassInstance As Object, ByVal ControlName As String, ByVal PropertyName As String) As Object Dim Result As Object Dim myType As Type = ClassInstance.GetType Dim myFieldInfo As FieldInfo = myType.GetField("_" & ControlName, BindingFlags.NonPublic Or _ BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.Instance) If Not myFieldInfo Is Nothing Then Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(myType) Dim myProperty As PropertyDescriptor = properties.Find(PropertyName, False) If Not myProperty Is Nothing Then Dim ctr As Object ctr = myFieldInfo.GetValue(ClassInstance) Try Result = myProperty.GetValue(ctr) Catch ex As Exception MsgBox(ex.Message) End Try End If End If Return Result End Function '以下根据控件名和属性名赋值 Public Function SetValueControlProperty(ByVal ClassInstance As Object, ByVal ControlName As String, ByVal PropertyName As String, ByVal Value As Object) As Object Dim Result As Object Dim myType As Type = ClassInstance.GetType Dim myFieldInfo As FieldInfo = myType.GetField("_" & ControlName, BindingFlags.NonPublic _ Or BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.Instance) '加"_"这个是特要紧的 If Not myFieldInfo Is Nothing Then Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(myType) Dim myProperty As PropertyDescriptor = properties.Find(PropertyName, False) '这里设为True就不用区分大小写了 If Not myProperty Is Nothing Then Dim ctr As Object ctr = myFieldInfo.GetValue(ClassInstance) '取得控件实例 Try myProperty.SetValue(ctr, Value) Result = ctr Catch ex As Exception MsgBox(ex.Message) End Try End If End If Return Result End Function '调用 '以下实现Label1.Text=TextBox1.Text,Label2.Text=TextBox2 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer For i = 1 To 2 Me.SetValueControlProperty(Me, "Label" & i.ToString, "Text", GetValueControlProperty(Me, "TextBox" & i.ToString, "Text")) Next i End Sub
缓存处理 http://blog.csdn.net/fengfangfang/archive/2006/09/06/1185077.aspx
消息处理 http://blog.csdn.net/fengfangfang/archive/2006/09/08/1194896.aspx
string fullyQualifiedClass = path + "." + className; // Using the evidence given in the config file load the appropriate assembly and class
return (IPetShopCacheDependency)Assembly.Load(path).CreateInstance(fullyQualifiedClass);
}
反射应用之一:根据控件名、属性名进行取值和赋值
'必须引用命名空间System.Reflection,System.ComponentModel '以下根据控件名和属性名取值 Public Function GetValueControlProperty(ByVal ClassInstance As Object, ByVal ControlName As String, ByVal PropertyName As String) As Object Dim Result As Object Dim myType As Type = ClassInstance.GetType Dim myFieldInfo As FieldInfo = myType.GetField("_" & ControlName, BindingFlags.NonPublic Or _ BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.Instance) If Not myFieldInfo Is Nothing Then Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(myType) Dim myProperty As PropertyDescriptor = properties.Find(PropertyName, False) If Not myProperty Is Nothing Then Dim ctr As Object ctr = myFieldInfo.GetValue(ClassInstance) Try Result = myProperty.GetValue(ctr) Catch ex As Exception MsgBox(ex.Message) End Try End If End If Return Result End Function '以下根据控件名和属性名赋值 Public Function SetValueControlProperty(ByVal ClassInstance As Object, ByVal ControlName As String, ByVal PropertyName As String, ByVal Value As Object) As Object Dim Result As Object Dim myType As Type = ClassInstance.GetType Dim myFieldInfo As FieldInfo = myType.GetField("_" & ControlName, BindingFlags.NonPublic _ Or BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.Instance) '加"_"这个是特要紧的 If Not myFieldInfo Is Nothing Then Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(myType) Dim myProperty As PropertyDescriptor = properties.Find(PropertyName, False) '这里设为True就不用区分大小写了 If Not myProperty Is Nothing Then Dim ctr As Object ctr = myFieldInfo.GetValue(ClassInstance) '取得控件实例 Try myProperty.SetValue(ctr, Value) Result = ctr Catch ex As Exception MsgBox(ex.Message) End Try End If End If Return Result End Function '调用 '以下实现Label1.Text=TextBox1.Text,Label2.Text=TextBox2 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer For i = 1 To 2 Me.SetValueControlProperty(Me, "Label" & i.ToString, "Text", GetValueControlProperty(Me, "TextBox" & i.ToString, "Text")) Next i End Sub
有心人,偶开一帖叫Asp.Net开发小技巧怎么样,到时候你也发几个啊
{
try
{
string assemblyQualifiedName = typeof(System.Windows.Forms.Form).AssemblyQualifiedName;
string assemblyInformation = assemblyQualifiedName.Substring(assemblyQualifiedName.IndexOf(","));
Type ty = Type.GetType(controlType + assemblyInformation);
Control newControl = (Control)System.Activator.CreateInstance(ty);
form.SuspendLayout();
newControl.Location = new System.Drawing.Point(positionX, positionY);
newControl.Name = ty.Name + form.Controls.Count.ToString();
form.Controls.Add(newControl);
form.ResumeLayout();
}
catch(Exception ex)
{
throw ex;
}
}
1、程序中可以不添加对类所处的程序集的引用
2、可以通过工厂方式创建对象,输入参数为类的名称
3、类似api的调用,直接从程序集中得到一个函数或方法
4、通过配置文件控制程序的菜单、工具栏项目等
反射使用的地方不是很多,现在在两个地方用到
1。工厂模式做的插件系统用,通过配置文件的方式加载DLL,通过反射创建实例。
2。数据dump,这个是用在日志系统里,当发生错误时,通过反射导出当前数据对象的值。
最全面的程序开发资料网www.mylinux.com.cn
包罗java,linux,数据库,安全等等技术资料,更有众多软件外包项目我们的qq群:15096318 学习程序的都可以来华资软件作为一家专业的软件公司,现公开承接各种软件外包项目.
www.mylinux.com.cn国内最大的网上软件加工厂,提供最完善的软件外包服务,采用流水型操作流程。中国软件业的发展不缺人才也不缺资金,缺的是人才的组织和管理,MyLinux平台的建设解决了软件人才的组织和管理问题,将每一项目最合适的软件开发人才以最有效率的形式组织在一起,从而取得1+1〉2的效果。
MyLinux(www. MyLinux.com.cn)由上海巨灵信息技术有限公司主办,是目前国内最大的网上软件加工厂,该网站将提供最完善的软件外包服务,采用流水型操作流程。详情请登陆www. MyLinux.com.cn
您可把您的具体要求发布在http://www.mylinux.com.cn/guiderAction.do?type=7上,并留下联系方式,我们网站的技术部门和客服会在第一时间审核安排.
场合一,做一程序名叫Reflector.exe,可以反编译出.net程序源码=============================================================
说的好啊~~~哈哈~~~
接点分
反射(也可以叫映射):是RUNTIME 的事,完成类的加载,类的分析,bean的自省等功能,JBUILDER为什么敲一个类的名称后面就列出了它的成员变量和方法供你选择?如果你想知道一个对象的某种方法被调用过多少次你用什么方法?这些知识可以让你从一行Exception就能分析出错误原因.以及自己实现classloader,安全管理等方面的工作.
反射授予了你的代码访问装载进JVM内的Java类的内部信息的权限,并且允许你编写在程序执行期间与所选择的类的一同工作的代码,而不是在源代码中。
a*b*3.2-2^ 这是简单的,还更复杂的通用函数和自定定义函数
a^2*sin(c-5)*max(a,b,c,d)-power(c,2)+sqrt(c)+abs(d)
等等一系列的函数,每个函数对应一处理这个函数的方法,不可能一个个去检查方法名,然后用 if ..else if ..else if 这么复杂
我就把每个函数的处理方法名写为f_函数名,如sin的对应的处理方法为f_sin 参数是一个ArrayList,返回一个double类型的数,这样进行处理时,只要根据函数名就可以用反射的机制来调用相关的处理函数,
这个计算库经过8个月使用和调试,已在我们公司广泛应用
需要的朋友可以联系我,[email protected] 可免费使用
http://community.csdn.net/Expert/topic/5016/5016795.xml?temp=.6735803
分太多了没什么用,不如做点事,你要是有兴趣可以一起商量怎么用掉啊