简体   繁体   English

为对象和程序集使用字符串名称

[英]Using a string name for an object and an assembly

I'm not great with reflection and I hope I chose the right category. 我对反思并不满意,希望我选择了正确的类别。 I have an interesting problem. 我有一个有趣的问题。 I have written a report using devexpress reports. 我已经使用devexpress报告编写了一个报告。 This involves a line of code like this: 这涉及到如下代码:

lxListData = New System.Collections.Generic.List(Of DLL.Report.ListData) 

ListData is report in an assembly external to the one where the above line is executed. ListData是在执行上述行的程序集之外的程序集中的报告。 The above line works fine and allows me to run the report. 上面的行工作正常,并允许我运行报告。

What I've done is put all the information for this report into a table as strings, That includes the type:" DLL.Report.ListData" 我所做的就是将该报告的所有信息作为字符串放入表中,其中包括类型:“ DLL.Report.ListData”

What I need is to change this string name into the type used in the line of code above. 我需要将此字符串名称更改为上面的代码行中使用的类型。 I have tried: 我努力了:

Dim type1 As Type = System.Reflection.Assembly.LoadWithPartialName("DLL.Report").GetType("DLL.Report.ListData")

and then changing the line above to: 然后将上面的行更改为:

lxListData = New System.Collections.Generic.List(Of type1) 

I've tried a few other techniques that are similar. 我尝试了其他一些类似的技术。 In all cases the "type1" variable is not seen as a type even though it looks like it is in the watch window. 在所有情况下,即使看起来在监视窗口中,“ type1”变量也不会被视为类型。

Any advice would be appreciated. 任何意见,将不胜感激。

Thanks, Neil 谢谢,尼尔

I've done some more research and found that this line: 我进行了更多研究,发现这条线:

Dim type1 As Type = System.Reflection.Assembly.LoadWithPartialName("DLL.Report").GetType("DLL.Report.ListData")

It returns a sytem.type in the variable type1, not type DLL.Report.ListData. 它在变量type1中返回sytem.type,而不是DLL.Report.ListData类型。 Within that variable are two properties: Name="ListData" and FullName="DLL.Report.ListData". 在该变量中有两个属性:Name =“ ListData”和FullName =“ DLL.Report.ListData”。 I was able to use some methods that were in type1 to get the properties of DLL.Report.ListData so type1 seems to be a pointer to DLL.Report.ListData. 我能够使用type1中的某些方法来获取DLL.Report.ListData的属性,因此type1似乎是DLL.Report.ListData的指针。 My problem is that I need a variable that is type DLL.Report.ListData. 我的问题是我需要一个类型为DLL.Report.ListData的变量。 Is there a way to take the Fullname string and return something that is the type DLL.Report.ListData instead of "system.type"? 有没有办法采用Fullname字符串并返回DLL.Report.ListData类型而不是“ system.type”的值?

Here's how: 这是如何做:

Dim list As IList = _
    Ctype(Activator.CreateInstance(GetType(List(Of )).MakeGenericType(type1)), IList)

Keep in mind that since you are creating this at run-time that you can't have strong-typing at compile-time. 请记住,由于是在运行时创建的,因此在编译时不能进行强类型化。 The best you can do is make this an IList . 您能做的最好的就是将其设置为IList

The other option, if you want to work with a strongly-typed list, is to call a generic method using reflection. 如果要使用强类型列表,另一种选择是使用反射调用泛型方法。 Work done within the method is strongly-typed. 该方法中完成的工作是强类型的。

Dim mi = Me.GetType().GetMethod("CreateList")
Dim gmi = mi.MakeGenericMethod(type1)
Dim list = Ctype(gmi.Invoke(Me, Nothing), IList)

Public Function CreateList(Of T) As List(Of T)
    ' Strongly-typed to `T` in here
    Return New List(Of T)()
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM