对于Linq Select语句,如何在运行时以通用方式构造子类?
假代码是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这(目前)不可能。来自 MSDN 的 C# 程序员指南,关于匿名类型:
I don't believe this is (currently) possible. From the C# Programmers Guide at MSDN, regarding anonymous types:
你不需要构造任何子类,运行时会为你生成一个匿名类
you need not construct any sub class, an anonymous class is generated for you at runtime
听起来您想创建一个扩展现有类型并注入自定义属性的动态类型。我非常确定这在单个 LINQ 语句中是不可能的(您需要 C#4.0 动态或一些反射疯狂)。
但是,您可以使用您喜欢的任何属性创建匿名类:
或者:
或者,为什么您的 ParentClass 上没有“附加属性”(类型对象或字典)属性,这样您就可以这样做:
您可以< /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:
Or:
Alternatively, why don't you have an "additional properties" (type object or Dictionary) property on your ParentClass so you could do this:
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.