对于Linq Select语句,如何在运行时以通用方式构造子类?

发布于 2024-09-07 10:01:21 字数 757 浏览 11 评论 0原文

假代码是:

  public class ParentClass
      { decimal quantity, 
        decimal price
      }

  IQueryable<ParentClass> parents;

  //SonClass is created at run time 
  //SonClass has some extra property, such as amount=quantity*price, which is defined by a string at run time

  IQueryable<SonClass> sons=parents.Select(p=>new SonClass(p){ attachedProperty1=..., attachedProperty2=...})

如果我有ParentClass的源代码,我就知道如何在运行时创建sonClass。实际上SonClass应该有一个适合Linq select语句的构造函数。 我尝试了以下假代码:

    public SonClass(ParentClass parent)
{
... 
}

当我们没有ParentClass的源代码时,如何编写通用代码?

或者,也许您有一种新方法可以获得相同的结果。期望的结果是使用 Linq Select 语句定义的额外只读属性动态创建 ParentClass 的子类。

先感谢您。

The fake code is:

  public class ParentClass
      { decimal quantity, 
        decimal price
      }

  IQueryable<ParentClass> parents;

  //SonClass is created at run time 
  //SonClass has some extra property, such as amount=quantity*price, which is defined by a string at run time

  IQueryable<SonClass> sons=parents.Select(p=>new SonClass(p){ attachedProperty1=..., attachedProperty2=...})

If I have the source code of ParentClass, I know how to create the sonClass at run time. Actually SonClass should have a suitable constructor for the Linq select statement.
I tried the following fake code:

    public SonClass(ParentClass parent)
{
... 
}

When we don't have the source code of ParentClass, how to write a generic code ?

Or, maybe you have a new way to get the same result. The desired results is to create a subclass of ParentClass dynamically with extra read only properties defined by Linq Select statement.

Thank you in advance.

Ying

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

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

发布评论

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

评论(3

失而复得 2024-09-14 10:01:21

我认为这(目前)不可能。来自 MSDN 的 C# 程序员指南,关于匿名类型:

匿名类型是引用类型
直接从对象派生。这
编译器给了它们一个名字
您的应用程序无法访问它。
从一般人的角度来看
语言运行时,匿名类型是
与任何其他参考没有什么不同
类型,但它不能转换为
除对象之外的任何类型。

I don't believe this is (currently) possible. From the C# Programmers Guide at MSDN, regarding anonymous types:

Anonymous types are reference types
that derive directly from object. The
compiler gives them a name although
your application cannot access it.
From the perspective of the common
language runtime, an anonymous type is
no different from any other reference
type, except that it cannot be cast to
any type except for object.

长途伴 2024-09-14 10:01:21

你不需要构造任何子类,运行时会为你生成一个匿名类

var sons=parents.Select(p=>new { attachedProperty1=p..., attachedProperty2=p...})
foreach (var son in sons){
     Console.WriteLine(son.PropertyName);
}

you need not construct any sub class, an anonymous class is generated for you at runtime

var sons=parents.Select(p=>new { attachedProperty1=p..., attachedProperty2=p...})
foreach (var son in sons){
     Console.WriteLine(son.PropertyName);
}
橙幽之幻 2024-09-14 10:01:21

听起来您想创建一个扩展现有类型并注入自定义属性的动态类型。我非常确定这在单个 LINQ 语句中是不可能的(您需要 C#4.0 动态或一些反射疯狂)。

但是,您可以使用您喜欢的任何属性创建匿名类:

var sons = parents.Select(p =>new { parentProperty1=p.Prop1, attachedProperty1=..., tachedProperty2=...});

或者:

var sons = parents.Select(p =>new { parent=p, attachedProperty1=..., tachedProperty2=...});

或者,为什么您的 ParentClass 上没有“附加属性”(类型对象或字典)属性,这样您就可以这样做:

var sons = parents.ForEach(p => p.AdditionalProps = new { attachedProperty1=..., tachedProperty2=...});

可以< /em> 编写一个扩展方法,使用反射来获取给定对象的所有公共属性,将它们与给定的匿名对象(您的“attachedProperty1 等对象”)合并,并返回一个包含所有合并属性的新匿名对象。然而,这将是一个糟糕的解决方案 - 更好的解决方案可能是返回一个 Dictionary。而不是匿名类型,因为即使您确实创建了合并的匿名类型,您也无法以类型安全的方式访问其属性。

It sounds like you want to create a dynamic type that extends an existing type and injects custom properties. I'm pretty sure that's impossible in a single LINQ statement (you'd need either C#4.0 dynamics or some reflection madness).

However, you can create anonymous classes with any properties you like:

var sons = parents.Select(p =>new { parentProperty1=p.Prop1, attachedProperty1=..., tachedProperty2=...});

Or:

var sons = parents.Select(p =>new { parent=p, attachedProperty1=..., tachedProperty2=...});

Alternatively, why don't you have an "additional properties" (type object or Dictionary) property on your ParentClass so you could do this:

var sons = parents.ForEach(p => p.AdditionalProps = new { attachedProperty1=..., tachedProperty2=...});

You could write an extension method that used reflection to grab all the public properties of a given object, merge them with a given anonymous object (your "attachedProperty1 etc. object") and return a new anonymous object with all merged properties. However, this would be a poor solution - a better solution would probably be to return a Dictionary<string, object> instead of an anonymous type because even if you did create a merged anonymous type you wouldn't be able to access its properties in a type-safe manner.

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