您现在的位置是:首页 > 电脑 > 

C#:Func与继承类型的构造函数(C#: Func with a ctructor of an inherited type)

2025-07-18 07:48:44
C#:Func与继承类型的构造函数(C#: Func with a ctructor of an inherited type) 正如我们所知,你可以像这样将一个构造函数指向Func<T> : Func<MyObject> ctructor = () => new MyObject(); var newObject
C#:Func与继承类型的构造函数(C#: Func with a ctructor of an inherited type)

正如我们所知,你可以像这样将一个构造函数指向Func<T> :

Func<MyObject> ctructor = () => new MyObject(); var newObject = ctructor();

但是有没有办法为你知道从MyObject继承的对象创建构造函数 ,但是你不知道它的确切类型?

Type inheritedObjectType = obj; // Parameter Func<MyObject> ctructor = () => new MyObject(); // as inheritedObjectType var newInheritedObject = ctructor; // Should now be of the inherited type

使用Activator或任何返回Object的答案不是一个选项。

编辑 :我不知道在编译时派生类型是什么类型。 我只有一个System.Type 。

As we know you can point to a ctructor as a Func<T> like this:

Func<MyObject> ctructor = () => new MyObject(); var newObject = ctructor();

But is there a way to make a ctructor for an object you know inherits from MyObject, but you don't know its exact type?

Type inheritedObjectType = obj; // Parameter Func<MyObject> ctructor = () => new MyObject(); // as inheritedObjectType var newInheritedObject = ctructor; // Should now be of the inherited type

An answer with using Activator or anything returning Object is not an option.

Edit: I don't know what type the derived type is at compile time. I only have a System.Type.

最满意答案

您可以使用表达式树来构建和编译将创建派生类型的委托:

Func<TBase> CreateDelegate<TBase>(Type derived) { var ctor = derived.GetCtructor(Type.EmptyTypes); if (ctor == null) { throw new ArgumentException("D'oh! o default ctor."); } var newExpression = Expression.Lambda<Func<TBase>>( (ctor, new Expression[0]), new ParameterExpression[0]); return newExpression.Compile(); }

你可以简单地调用它,如下所示:

Func<MyBase> create = CreateDelegate<MyBase>(typeof(Derived)); MyBase instance = create();

当您缓存该代理时,您将获得最佳性能。

You can use expression trees to build and compile a delegate that will create your derived type:

Func<TBase> CreateDelegate<TBase>(Type derived) { var ctor = derived.GetCtructor(Type.EmptyTypes); if (ctor == null) { throw new ArgumentException("D'oh! o default ctor."); } var newExpression = Expression.Lambda<Func<TBase>>( (ctor, new Expression[0]), new ParameterExpression[0]); return newExpression.Compile(); }

You can simply call it as follows:

Func<MyBase> create = CreateDelegate<MyBase>(typeof(Derived)); MyBase instance = create();

When you cache that delegate, you will get maximum performance.

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/697372.html

相关标签:无
上传时间: 2023-08-27 09:47:31
留言与评论(共有 8 条评论)
本站网友 猕猴桃功效
18分钟前 发表
你可以像这样将一个构造函数指向Func<T> : Func<MyObject> ctructor = () => new MyObject(); var newObject = ctructor(); 但是有没有办法为你知道从MyObject继承的对象创建构造函数
本站网友 英伟达显卡性能排行
19分钟前 发表
但是你不知道它的确切类型? Type inheritedObjectType = obj; // Parameter Func<MyObject> ctructor = () => new MyObject(); // as inheritedObjectType var newInheritedObject = ctructor; // Should now be of the inherited type 使用Activator或任何返回Object的答案不是一个选项
本站网友 苦杏仁
10分钟前 发表
new ParameterExpression[0]); return newExpression.Compile(); } 你可以简单地调用它
本站网友 蓬莱公园二手房
3分钟前 发表
如下所示: Func<MyBase> create = CreateDelegate<MyBase>(typeof(Derived)); MyBase instance = create(); 当您缓存该代理时
本站网友 湖北省旅游学校
5分钟前 发表
As we know you can point to a ctructor as a Func<T> like this
本站网友 洛阳电影
13分钟前 发表
如下所示: Func<MyBase> create = CreateDelegate<MyBase>(typeof(Derived)); MyBase instance = create(); 当您缓存该代理时
本站网友 web应用防火墙排名
10分钟前 发表
编辑 :我不知道在编译时派生类型是什么类型