反射:Type and Reflection
2009-06-09 11:52
176 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace House
{
public class Class1
{
private string name;
private int age;
public Class1(string Name, int Age)
{
name = Name;
age = Age;
}
public void ChangeName(string NewName)
{
name = NewName;
}
public void ChangeAge(int NewAge)
{
age = NewAge;
}
public override string ToString()
{
return string.Format("Name: {0}, Age: {1}", name, age);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Assembly tmpAss = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "House.dll");
Type[] tmpTypes = tmpAss.GetTypes();
foreach (Type tmpType in tmpTypes)
{
ConstructorInfo[] tmpConsInfos = tmpType.GetConstructors();
foreach (ConstructorInfo tmpConsInfo in tmpConsInfos)
{
ParameterInfo[] tmpParamInfos = tmpConsInfo.GetParameters();
object[] tmpParams = new object[tmpParamInfos.Length];
for (int i = 0; i < tmpParamInfos.Length; i++)
{
tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
if (tmpParamInfos[i].ParameterType.FullName == "System.String")
{
tmpParams[i] = "Clark";
}
}
object tmpObj = tmpConsInfo.Invoke(tmpParams);
Console.WriteLine(tmpObj);
foreach (MethodInfo tmpMethod in tmpType.GetMethods())
{
tmpParamInfos = tmpMethod.GetParameters();
tmpParams = new object[tmpParamInfos.Length];
for (int i = 0; i < tmpParamInfos.Length; i++)
{
tmpParams[i] = tmpAss.CreateInstance
(tmpParamInfos[i].ParameterType.FullName);
if (tmpParamInfos[i].ParameterType.FullName == "System.String")
{
tmpParams[i] = "Clark Zheng";
}
if (tmpParamInfos[i].ParameterType.FullName == "System.Int32")
{
tmpParams[i] = 27;
}
}
tmpMethod.Invoke(tmpObj, tmpParams);
}
Console.WriteLine(tmpObj);
Console.ReadKey();
}
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace House
{
public class Class1
{
private string name;
private int age;
public Class1(string Name, int Age)
{
name = Name;
age = Age;
}
public void ChangeName(string NewName)
{
name = NewName;
}
public void ChangeAge(int NewAge)
{
age = NewAge;
}
public override string ToString()
{
return string.Format("Name: {0}, Age: {1}", name, age);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Assembly tmpAss = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "House.dll");
Type[] tmpTypes = tmpAss.GetTypes();
foreach (Type tmpType in tmpTypes)
{
ConstructorInfo[] tmpConsInfos = tmpType.GetConstructors();
foreach (ConstructorInfo tmpConsInfo in tmpConsInfos)
{
ParameterInfo[] tmpParamInfos = tmpConsInfo.GetParameters();
object[] tmpParams = new object[tmpParamInfos.Length];
for (int i = 0; i < tmpParamInfos.Length; i++)
{
tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
if (tmpParamInfos[i].ParameterType.FullName == "System.String")
{
tmpParams[i] = "Clark";
}
}
object tmpObj = tmpConsInfo.Invoke(tmpParams);
Console.WriteLine(tmpObj);
foreach (MethodInfo tmpMethod in tmpType.GetMethods())
{
tmpParamInfos = tmpMethod.GetParameters();
tmpParams = new object[tmpParamInfos.Length];
for (int i = 0; i < tmpParamInfos.Length; i++)
{
tmpParams[i] = tmpAss.CreateInstance
(tmpParamInfos[i].ParameterType.FullName);
if (tmpParamInfos[i].ParameterType.FullName == "System.String")
{
tmpParams[i] = "Clark Zheng";
}
if (tmpParamInfos[i].ParameterType.FullName == "System.Int32")
{
tmpParams[i] = 27;
}
}
tmpMethod.Invoke(tmpObj, tmpParams);
}
Console.WriteLine(tmpObj);
Console.ReadKey();
}
}
}
}
}
相关文章推荐
- torque游戏引擎DTS文件的LOD实现(含reflection反射材质演示)-The LOD and reflection in DTS files
- QT AND C++ REFLECTION,利用QT简化C++的反射实现
- 当用到反射的程序部署时出现System.Data - System.Reflection.ReflectionTypeLoadException: Unable to load one or more
- Reflection反射,Assembly程序集,Type类型类,AssemblyQualifiedName程序集的限定名,DataTable和IList接口互相转化
- .Net 中的反射(序章) - Part.1(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part1.aspx)
- Qt and C++ Reflection,利用Qt简化C++的反射实现
- .Net 中的反射(查看基本类型信息) - Part.2 (转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part2.aspx)
- ClassReflection(Copy bean and list) 运用反射来实现bean或list的弱引用
- .Net 中的反射(反射特性) - Part.3(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part3.aspx)
- .Net 中的反射(动态创建类型实例) - Part.4(转自http://www.tracefact.net/CLR-and-Framework/Reflection-Part4.aspx)
- Scala Reflection - Mirrors,ClassTag,TypeTag and WeakTypeTag
- Scala Reflection - Mirrors,ClassTag,TypeTag and WeakTypeTag
- 从Atlas到Microsoft ASP.NET AJAX(3) - Class and Type Definition, Reflection APIs
- Type.GetType()反射另外项目中的类时返回null的解决方法
- Java反射经典实例 Java Reflection Cookbook
- 使用SharePoint Web Serivce(3)--Add a new Item with HyperLink and User Type Column
- C++ stander library--type traits and type utility
- ADF Issue: triggerType="action" blocks action and actionListener of parent
- error CS1061: Type `UnityEngine.Mesh' does not contain a definition for `GetTriangleStrip' and no ex
- JAVA的内省(introspector)与反射(reflection)