如何以编程方式访问 .net 应用程序中可用的数据类型

发布于 2025-01-06 02:24:32 字数 1333 浏览 1 评论 0原文

简短的问题:有没有办法以组的形式访问 .net 应用程序中可用的数据类型(从而使用 LINQ 查询它们)。像这样......

Dim myType = from l in myapplication.availabledatatypes where l.name="name of type I want" select l

更长的解释: 我正在尝试使用 Linq to SQL 将大量旧记录从表移动到相同的“存档”表中。作为该过程的一部分,我需要从活动表中选择记录并将它们转换为(相同的)存档类型。我正在尝试使用一种方法干净地完成此操作,该方法可以通过使用反射用于所有不同的表/存档表对,遵循这个优秀的指南 http://michaelmerrell.com/2010/ 08/converting-a-linq-object-to-a-similar-object/

我的转换方法将采用对象类型的输入并将其转换为对象类型的输出,这然后,我计划在调用转换方法的方法中转换为所需的输出类型。我能够获取 datacontext 的属性,该属性与我想要转换的类型同名,但我似乎无法以编程方式提取具有相同名称的类型。

Dim typeIamLookingFor as propertyInfo= from l in mydatacontext.getproperties where l.name.contains("archive") and l.name.contains(input.gettype.name) select l

如何从命名空间中可用的类型中选择名称与 typeIamLookingFor 相同的类型。 的类型

显然 .getType 返回类型 PropertyInfo,但我真正想要的是名称 = typeiamlookingfor.name Edit 感谢您的建议。 Type.GetType 让我更接近,但编译器现在说对象必须实现 IConvertible。我将调查这个不同的错误并接受这个答案。

Dim output as object
Dim typeName as string = "namespace." & inputObject.gettype.name & "_Archive"
output = output = Convert.ChangeType(output, Type.GetType(typeName))  //generates error: Object Must Implement IConvertible

Short question: is there a way to access the datatypes available in .net application as a group (and hence query them with LINQ). Like this ...

Dim myType = from l in myapplication.availabledatatypes where l.name="name of type I want" select l

Longer explanation:
I'm trying to move lots of old records from tables into identical 'archive' tables with Linq to SQL. As a part of that process, I need to select records from the live table and convert them to the (identical) archive type. I'm trying to do this cleanly with a single method that can be used for all the different table/archive table pairs by using reflection, following this excellent guide
http://michaelmerrell.com/2010/08/converting-a-linq-object-to-a-similar-object/

My conversion method will take an input of type object and convert it to an output of type object, which I then plan to cast to the required output type in the method that calls the converting method. I am able to get the property of datacontext which has the same name as the type I want to convert to, but I can't seem to programatically pull the type with the same name.

Dim typeIamLookingFor as propertyInfo= from l in mydatacontext.getproperties where l.name.contains("archive") and l.name.contains(input.gettype.name) select l

How do I pick, from the types available in my namespace, the one whose name equals that of typeIamLookingFor. Obviously .getType returns the type PropertyInfo, but what I really want is the type with the name = typeiamlookingfor.name

Edit
Thanks for suggestions. Type.GetType gets me closer, but compiler now saying Object Must Implement IConvertible. I am going to investigate this different error and accept this answer.

Dim output as object
Dim typeName as string = "namespace." & inputObject.gettype.name & "_Archive"
output = output = Convert.ChangeType(output, Type.GetType(typeName))  //generates error: Object Must Implement IConvertible

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剧终人散尽 2025-01-13 02:24:32

我认为你的解决方案过于复杂。根据您的描述,您可能只需要 Type.GetType()

Type typeIamLookingFor = Type.GetType("archive." + typeName)

还有更多吗?

I think you are over-complicating your solution. Based on your description, you probably just need Type.GetType():

Type typeIamLookingFor = Type.GetType("archive." + typeName)

Is there more to it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文