最近想做一个插件式的软件给公司的监控用,初步的想法是使用C#去反射Dll,Invoke其中的方法。此文仅供开发参考,不涉及原理,98%的代码以及2%的废话。
测试Solution是这么建的(.NET FRAMEWORK 4.5.1):
Person类代码:
using System;
using System.Collections.Generic;
using System.Linq; namespace PersonMoudle
{
public class Person
{
public Person()
{
Name = "Sirius";
Age = ;
Height = ;
Sex = "Middle";
}
public Person(string name, int age, float height, string sex)
{
Name = name;
Age = age;
Height = height;
Sex = sex;
} public string Name { get; set; }
public int Age { get; set; }
public float Height { get; set; }
public string Sex { get; set; } public void Speak(string words)
{
Console.WriteLine(words);
}
private string GetMyName()
{
return Name.Trim();
} public string GetMySex()
{
return Sex;
} public List<string> BeenCity()
{
return new List<string>
{
"Beijing",
"Jinan",
"NewYork"
};
} public List<string> BennCity(int count)
{
return new List<string>
{
"Beijing",
"Jinan",
"NewYork"
}.Take(count).ToList();
}
}
}
控台中反射获取信息代码(控台引用类库项目,也可以load dll,后面的篇幅会写):
using System;
using System.Linq;
using PersonMoudle; namespace ReflectionTest
{
class Program
{
static void Main(string[] args)
{
var count = ;//public count set; var person = new Person();
var personType = person.GetType();
Console.WriteLine("类型名: " + personType.Name);
Console.WriteLine("类全名: " + personType.FullName);
Console.WriteLine("命名空间名: " + personType.Namespace);
Console.WriteLine("程序集名: " + personType.Assembly);
Console.WriteLine("模块名: " + personType.Module.Name);
Console.WriteLine("是否支持泛型: "+personType.IsConstructedGenericType); //获取构造函数信息
var conInfo = personType.GetConstructors();
Console.WriteLine(personType.FullName + "共有" + conInfo.Count() + "个构造函数");
foreach (var constructorInfo in conInfo)
{
count += ;
Console.WriteLine("第" + count + "个构造函数:");
var paramsInfo = constructorInfo.GetParameters();
Console.WriteLine(paramsInfo.Any() ? "参数列表:" : "无参数。");
foreach (var parameterInfo in paramsInfo)
{
Console.WriteLine("第" + (parameterInfo.Position + ) + "参数名:" + parameterInfo.Name + ", 参数类型:" +
parameterInfo.ParameterType.FullName);
}
}
count = ; //获取事件信息
var eventInfo = personType.GetEvents();
Console.WriteLine(personType.FullName+"共有"+eventInfo.Count()+"个事件"); //获取字段信息
var fields = personType.GetFields();
Console.WriteLine(personType.FullName + " has " + fields.Count() + "fields.");
foreach (var fieldInfo in fields)
{
count += ;
Console.WriteLine("The "+count+"st field is"+fieldInfo.Name+", type is "+fieldInfo.FieldType);
}
count = ; //获取接口信息
var ifaces = personType.GetInterfaces();
Console.WriteLine(personType.FullName + " has " + ifaces.Count() + " interfaces."); //获取成员信息
var members = personType.GetMembers();
Console.WriteLine(personType.FullName+" has "+members.Count()+" members.");
foreach (var memberInfo in members)
{
count += ;
Console.WriteLine("The " + count + "st member name is " + memberInfo.Name + ", type is " +
memberInfo.ReflectedType);
}
count = ; //获取方法信息
var methods = personType.GetMethods();
Console.WriteLine(personType.FullName+" has "+methods.Count()+" methods.");
foreach (var methodInfo in methods)
{
count += ;
var paramInfos = methodInfo.GetParameters();
Console.WriteLine("The " + count + "st method is " + methodInfo.Name + ", return type is " +
methodInfo.ReturnType + ", param count is " + paramInfos.Count()); Console.WriteLine(paramInfos.Any() ? "参数列表:" : "无参数。");
foreach (var parameterInfo in paramInfos)
{
Console.WriteLine("param name:" + parameterInfo.Name + ", param type: " +
parameterInfo.ParameterType);
}
}
count = ; //propertites
var pros = personType.GetProperties();
Console.WriteLine(personType.FullName+" has "+pros.Count()+ "public properties."); Console.ReadKey();
}
}
}
下一篇讲如何调用方法。