C#的反射详解(二)

本文介绍了如何在运行时动态加载并使用类型,通过反射机制实现晚期绑定,从而调用DLL中的方法。此外,还展示了如何获取并设置自定义属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态添加和使用类型 

反射提供了由语言编译器 (例如 Microsoft Visual Basic .NET 和 JScript)用来实现隐式晚期绑定的基础结构。绑定是查找与唯一指定的类型相对应的声明(即实现)的过程。由于此过程在运行时而不是在编译时发生,所 以称作晚期绑定。Visual Basic .NET 允许您在代码中使用隐式的晚期绑定;Visual Basic 编译器将调用一个帮助器方法,该方法使用反射来获取对象类型。传递给帮助器方法的参数有助于在运行时调用正确的方法。这些参数包括:对其调用方法的实例 (对象),被调用方法的名称(字符串),以及传递给被调用方法的参数(对象数组)。

以下示例是动态调用动态链接库中的GetDataSet方法,该方法需要参数string userID
3. 访问自定义属性
       访问自定义属性和动态添加和使用类型一样.
/**//// <summary>
        
/// 执行公式的运算
        
/// </summary>
        
/// <param name="value">需要计算的原始值</param>
        
/// <param name="assessment">考核名称</param>
        
/// <returns></returns>
        public float ExeExpression(string assessment, params object[] values)
        
...{

            
string expression = this.GetExpression(assessment);
            
//从当前应用程序加载此dll  
            string path = "";
            
if (System.Environment.CurrentDirectory + @"" == AppDomain.CurrentDomain.BaseDirectory)//Windows应用程序则相等
            ...{
                 path
= AppDomain.CurrentDomain.BaseDirectory;
             }

            
else
            
...{
                 path
= AppDomain.CurrentDomain.BaseDirectory + @"Bin";
             }

            
string dllPath = path + expression + ".dll";
             System.Reflection.Assembly assmble
= System.Reflection.Assembly.LoadFile(dllPath);

            
string typeFullName = expression + ".ExpressionClass";
             Type tmpType
= assmble.GetType(typeFullName);
             System.Reflection.MethodInfo tmpMethod
= tmpType.GetMethod("ExeExpression");
             System.Reflection.PropertyInfo tmpProperty
= tmpType.GetProperty("ConnectionString");

            
//创建对象,设置属性值,并调用方法
            object tmpobj = assmble.CreateInstance(typeFullName);
             tmpProperty.SetValue(tmpobj, connectionString,
null);
            
float result = 0;
             result
= (float)tmpMethod.Invoke(tmpobj, values);
            
return result;

         }
Assembly assembly;
Type type;

string dllPath = @"D: estPowerSpace.VCP.Utility.dll";
try

...{
      assembly
= Assembly.LoadFile(dllPath);
      type
= assembly.GetType("PowerSpace.VCP.Utility.cMyString",true,true);//cMyResult
}
catch(FileNotFoundException)
...{

Response.Write(
"Could not load Assembly: ""+ dllPath +""");

      Return
null;

}

catch(TypeLoadException)

...{

Response.Write(
"Could not load Type: "string" from assembly: "" + dllPath + """); return null;


}


MethodInfo method
= type.GetMethod("TestInvoke");

object obj = Assembly.GetAssembly(type).CreateInstance("PowerSpace.VCP.Utility.GetDataSet");


object s = method.Invoke(obj,new object[]...{"jiangli"});

DataSet ss
= (DataSet)s;

assembly
= null;
type
= null;
method
=null;
return ss;




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值