【发布时间】:2013-04-17 14:22:20
【问题描述】:
我有一个带有可选参数的构造函数。 我想要一个表达式来调用该构造函数而不提供可选参数(我的意思是让对象使用参数的默认值构造)。
我在这里读到 An expression tree may not contain a call or invocation that uses optional arguments 这是不可能的。
我是说
var ctorInfo = getIt;
var f = Expression.Lambda<Func<T>>(Expression.New(ctorInfo)).Compile();
System.TypeInitializationException 失败。
好的,我将传递默认值。但是如何获取参数的默认值呢?
ctorInfo.GetParameters().Select(??
动机:学习目的,没有实际应用。
编辑:编辑了 expression-tree 标签,因为它不在构建表达式的上下文中,一般也有效。
【问题讨论】:
标签: c# constructor default-value optional-parameters