通用声明VB.NET或C#中的System.Type实例 [英] System.Type instance in generic declaration VB.NET or C#

查看:67
本文介绍了通用声明VB.NET或C#中的System.Type实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很沮丧.请帮忙!我用谷歌搜索直到手指受伤!

我有一个类型的实例(通过反射创建),需要在泛型类的声明中使用它的类型.例子:

I''m stumped. Please help! I''ve googled till my fingers hurt!

I have an instance of a type (created through reflection) and need to use it''s type in the declaration of a generic class. Emample:

'actually created with reflection, but for the sake of simplicity
dim myObject as Foo 

dim myGeneric as SomeGenericClass.GetAll(of myObject.getType)() 'does not work
dim myGeneric as SomeGenericClass.GetAll(of Foo)() 'works



在设计期间,我不会知道类的类型.它是在运行时从数据库加载的.我正在使用反射来查找和创建正确的类的实例,但是我不能在通用声明中使用它的类型.这有可能吗?我不愿使用Select语句,因为该类有一百多种可能性,并且我们计划经常更改的类列表.

对于任何对结局感到好奇的人,该程序将连接到SQL Server,使用实体框架读取一个数据库,然后使用CRM SDK将数据移动到CRM Webapp(这与查询查询的实体框架非常相似)被创建).


更新

为了澄清:我正在使用实体框架从SQL数据库获取数据,并使用CRM SDK连接到我们的CRM项目以将来自SQL数据库的数据放入其中.我们创建了一个基于IRepository的类.实体框架基于数据库中的表生成类和属性.此数据保存在存储库中.因此,如果我的SQL数据库有一个名为"Employee"的表,该表具有两列"ID"和"Name",我将使用以下内容获取有关该雇员的信息.在此示例中,我们的存储库称为"SQLdata".



I won''t know the the class type during design time. It''s loaded from a database at runtime. I''m using reflection to find and create an instance of the correct class, but I can''t use it''s type in the generic declaration. Is this even possible? I''d hate to resort to a Select statement as the class has over a hundred possibilities and we plan on the list of classes changing often.

For anyone curious as to the endgame, the program is connecting to SQL Server, reading one database using the Entity Framework, and then moving that data to a CRM Webapp using the CRM SDK (which is very similar to the entity framework in the way queries are created).


Update

For clarification: I am using the entity framework to get data from an SQL database and the CRM SDK to connect to our CRM project to put the data from the SQL database in. We made a class based on IRepository. The Entity Framework generates classes and properties based on the tables in our database. This data is held in the repository. So if my SQL database had a table called "Employee" with two columns "ID" and "Name", I would use the following to get information on that employee. In this example, our repository is called "SQLdata".

'get all employees
dim emps as list(of Employee) = SQLdata.GetAll(of Employee)()

'write employees out to console window
for each curEmp as Employee in emps
  console.writeLine("ID: " & curEmp.ID & " Name: " & curEmp.Name)
next



很好现在,假设您不知道需要员工的信息.有一个包含"Employee.Name","Supervisor.Name","Project.ID"等的字符串列表.我必须加载此列表,找出哪个实体框架类是匹配项,并获取数据为了它.使用反射,我已经能够上课.但是我不能在存储库的GetAll()方法中使用它.这就是我想要做的:



That works fine. Now imagine that you don''t know that you need the employee''s information. There''s a list of strings that contains "Employee.Name", "Supervisor.Name", "Project.ID", etc. I have to load this list, figure out which entity framework class is a match, and get the data for it. Using reflection I''ve been able to get the class. But I can''t use it in the GetAll() method of the repository. This is what I want to do:

'get the class type from the dictionary created by my reflection code
'it takes a string and returns the System.Type that string represents
dim myType as System.Type = myTypeDictionary("Employee") 

'get all records for that type
dim emps as list(of myType) = SQLdata.GetAll(of myType)()'does not work :(



上面的代码说" myType"未定义."

请保存我代码项目!我相信您的出色!!!



The above code says "''myType'' is not defined."

Please save me Code Project! I trust in your awesomeness!!

推荐答案

不确定您要做什么

如果foobar带有一个通用参数t,并且您想在该例程中创建t的实例,您是否有一个常规的let调用?

例如

not sure what you are after

is it that you have a rountine lets call if foobar which takes a generic argument t and you want to create an instance of t inside that rountine?

e.g.

 public sub foobar(of t)(byref Data as t)
   'This now creates a variable which will then become an object
   'of the type t.
   dim myObject as object = activator.createinstance(gettype(t))

 end sub

'You would call this rotine like so, from somewhere in your code
dim stringData as string = "SIMON"
foobar(stringData)


这篇关于通用声明VB.NET或C#中的System.Type实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆