码迷,mamicode.com
首页 > Windows程序 > 详细

C#反射

时间:2015-08-11 22:59:54      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

 

今天学的C#反射,虽然刚开始有点头晕,但明白了还是很简单滴.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace Reflection
{
class Program
{
static void Main(string[] args)
{
Assembly assembly = Assembly.LoadFrom("Reflection.exe");//程序集
Console.WriteLine(assembly);
Console.WriteLine();

Type type1 = typeof(Program);
Console.WriteLine(type1);
Console.WriteLine();

Program program = new Program();
Type type2 = program.GetType();
Console.WriteLine(type2);
Console.WriteLine();

Type type3 = Type.GetType("Reflection.Program", true, true);
Console.WriteLine(type3);
Console.WriteLine();

Type type4 = assembly.GetType("Reflection.Program");
Console.WriteLine(type4);
Console.WriteLine();

Type[] type5 = assembly.GetTypes();
foreach (var item in type5)
{
Console.WriteLine(item);
}
Console.WriteLine();

Type type6 = typeof(Test);
MethodInfo[] methodinfo=type6.GetMethods();
foreach (var item in methodinfo )
{
Console.WriteLine(item);
}
Console.WriteLine();

Assembly assembly1 = Assembly.Load("Reflection");
Type type7 = assembly.GetType("Reflection.Test");
object obj = Activator.CreateInstance(type7);
MethodInfo methodinfo1 = type7.GetMethod("Say");
methodinfo1.Invoke(obj, new object[] { "tc" });
Console.WriteLine();

Type type8 = typeof(Test);
PropertyInfo[] propertyinfo = type8.GetProperties();
foreach (var item in propertyinfo)
{
Console.WriteLine(item);
}
Console.WriteLine();

Assembly assembly2 = Assembly.Load("Reflection");
Type type9 = assembly.GetType("Reflection.Test");
object obj1 = Activator.CreateInstance(type9);
PropertyInfo propertyinfo1 = type9.GetProperty("Name");
var name = propertyinfo1.GetValue(obj,null);
Console.WriteLine();


}
}
class Test
{
string name;
public void Say(string name)
{
Console.WriteLine("hello {0}", name);
}
public string Name
{
get { return name; }
set { this.name = value; }
}
}
}

C#反射

标签:

原文地址:http://www.cnblogs.com/qtc-zyl/p/4722282.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!