C# 反射问题

来源:百度知道 编辑:UC知道 时间:2024/05/22 14:55:59
Type propertyType = prop.GetType();
prop是一个PropertyInfo实例.
假定现在得到Type为string,怎样用propertyType建一个string的变量?
没有人发个代码上来吗?
实际就两三行而已..
晕了...
我就是不会用那东西..

没看太明白...
propertyType.Name现在的值是string
怎么创建一个string的对象在上下文中可以调用的...
我做的是一个动态排序的程序.
在combox中选择结构的属性,然后通过选择的属性把存在List中的结构变量根据选择的属性排序..
public class ComparerOfUser : IComparer<User>
{
private string _property;//传进来的从combox中选择的属性

public ComparerOfUser(string prop)
{
_property = prop;
}
public int Compare(User x, User y)//User是结构名
{
Type objTypeX = x.GetType();
PropertyInfo propertyx = objTypeX.GetProperty(_property);
Object propx = propertyx.GetValue(x, null);
Type propertyTypeX = propx.GetType();//假设现在得到的是string类型的一个属性,怎么在下文中提取并比较?
return 0;
}

这个问题问的很有学问,用反射来创建!

string path = ConfigurationManager.AppSettings["CacheDependencyAssembly"];
string fullyQualifiedClass = path + "." + className;

// Using the evidence given in the config file load the appropriate assembly and class
return (IPetShopCacheDependency)Assembly.Load(path).CreateInstance(fullyQualifiedClass);

d

Assembly.CreateInstance....