【发布时间】:2013-04-14 03:13:21
【问题描述】:
我几乎没有嵌套类,例如“BlueprintsManager.WorkingStandardBlueprint”,以及具有它们实例的 ArrayList。我想将它们作为参数传递给方法,例如:
private void ShowBlueprints(string class_str, ArrayList class_array)
{
// class_str would be passed as "BlueprintsManager.WorkingStandardBlueprint"
// how here I can access class_str as a class and cast class_array to it, to access some variables.
// for example, I need to access some BlueprintsManager.WorkingStandardBlueprint public variables.
}
我一直在搞反射,泛型,但我仍然无法让它工作。
谢谢。
【问题讨论】:
-
那么
class_str是类名,class_array是什么?它是对象的集合,其中一个(或全部)属于class_str定义的类型??? -
是的,class_array 将是类实例的列表,所以如果将 class_str 作为“BlueprintsManager.WorkingStandardBlueprint”传递,我想将 class_array[0] 转换为 class_str。
-
你用的是什么版本的c#
-
如果是 4.0 及以上版本试试这个动态 printObj=class_array[0] 并访问属性为 printObj.property1 ,你不需要传递类名
-
不幸的是它是 2.x :-)
标签: c# generics reflection types casting