class Class5 { static void Main(string[] args) { MyClass m = new MyClass(); Type type = m.GetType(); Console.WriteLine("类型名:" + type.Name); Console.WriteLine("类全名:" + type.FullName); Console.WriteLine("命名空间名:" + type.Namespace); Console.WriteLine("程序集名:" + type.Assembly); Console.WriteLine("模块名:" + type.Module); Console.WriteLine("基类名:" + type.BaseType); Console.WriteLine("是否类:" + type.IsClass); Console.WriteLine("类的公共成员:"); MemberInfo[] memberInfos = type.GetMembers();//得到所有公共成员 foreach (var item in memberInfos) { Console.WriteLine("{0}:{1}", item.MemberType, item); } }
class MyClass { public string m; public void test() { } public int MyProperty { get; set; } }
public static void Main() { //获取当前执行代码的程序集 Assembly assem = Assembly.GetExecutingAssembly();
Console.WriteLine("程序集全名:" + assem.FullName); Console.WriteLine("程序集的版本:" + assem.GetName().Version); Console.WriteLine("程序集初始位置:" + assem.CodeBase); Console.WriteLine("程序集位置:" + assem.Location); Console.WriteLine("程序集入口:" + assem.EntryPoint);
Type[] types = assem.GetTypes(); Console.WriteLine("程序集下包含的类型:"); foreach (var item in types) { Console.WriteLine("类:" + item.Name); } }
private int factor; public Class5(int f) { factor = f; }
public int SampleMethod(int x) { Console.WriteLine("/nExample.SampleMethod({0}) executes.", x); return x * factor; } public static void Main() { Assembly assem = Assembly.GetExecutingAssembly(); Console.WriteLine("Assembly Full Name:"); Console.WriteLine(assem.FullName);
// The AssemblyName type can be used to parse the full name. AssemblyName assemName = assem.GetName(); Console.WriteLine("/nName: {0}", assemName.Name); Console.WriteLine("Version: {0}.{1}",assemName.Version.Major, assemName.Version.Minor); Console.WriteLine("/nAssembly CodeBase:"); Console.WriteLine(assem.CodeBase); // 从程序集众创建一个Example实例并且用object类型的引用o指向它,同时调用一个输入参数的构造函数 Object o = assem.CreateInstance("ConsoleApplication2.Example", false,BindingFlags.ExactBinding,null, new Object[] { 2 }, null, null); //构造Example类的一个晚绑定的方法SampleMethod MethodInfo m = assem.GetType("ConsoleApplication2.Example").GetMethod("SampleMethod"); //调用刚才实例化好的Example对象o中的SampleMethod方法,传入的参数为42 Object ret = m.Invoke(o, new Object[] { 42 }); Console.WriteLine("SampleMethod returned {0}.", ret);
Console.WriteLine("/nAssembly entry point:"); Console.WriteLine(assem.EntryPoint); }
}