技术开发 频道

.NET反射技术封装

  下面的代码对上面的一些方法进行测试,由于手头只有一个Fetion SDK.dll的文件,所以就拿他开刀了,实现简单的登录,登录后发送一条测试信息到我的手机,为了安全略去了某些信息。

using System;
using System.Collections.Generic;
using System.Text;
using UsefulUtility;
using System.Reflection;

namespace ConsoleApplication1
{
    
class Program
    {
        
static object sdk;
        
static Assembly asm;
        [STAThread]
        
static void Main(string[] args)
        {
            
// 载入程序集
            asm = Assembly.LoadFrom("Fetion SDK.dll");
            
// 获取Fetion SDK的类型
            Type type = Invoker.GetType(asm, "com.hetaoos.FetionSDK.FetionSDK");
            
// 实例化sdk
            sdk = Invoker.CreateInstance(type);
            
// 获取账号管理的属性
            object accountManager = Invoker.GetProperty(sdk, "AccountManager");
            
// 设置用户名和密码
            Invoker.CallMethod(accountManager, "FillUserIdAndPassword", new object[] { "手机号码", "飞信密码", false });
            
// 监听事件
            Invoker.AddEventHandler(sdk, "SDK_UserSatusChange", Invoker.GetMethod(typeof(Program), "sdk_SDK_UserSatusChange"), null);
            
// 调用登录方法
            Invoker.CallMethod(accountManager, "Login");
        }
        
static void Hello()
        {
            Console.WriteLine(
"hello in Hello");
        }

        
static void sdk_SDK_UserSatusChange(object sender, EventArgs e)
        {

            Console.WriteLine(Invoker.GetProperty(e,
"NewStatus"));
            Console.WriteLine(Invoker.GetSaticField(Invoker.GetType(asm,
"Imps.Client.UserAccountStatus"), "Logon"));
            
// 这里用==不好使,要用Equals,不知道为什么
            if (Invoker.GetProperty(e, "NewStatus").Equals(Invoker.GetSaticField(Invoker.GetType(asm, "Imps.Client.UserAccountStatus"), "Logon")))
            {
                Console.WriteLine(
"hello");
                
object contactControl = Invoker.GetProperty(sdk, "ContactControl");
                
object sendSMS = Invoker.GetProperty(contactControl, "SendSMS");
                Invoker.CallMethod(sendSMS,
"SendSMS", "要发送信息的飞信号或手机号码", "hello, a test");
            }
        }
    }
}
0
相关文章