本文整理汇总了C#中System.Linq.Expressions.ListInitExpression类的典型用法代码示例。如果您正苦于以下问题:C# ListInitExpression类的具体用法?C# ListInitExpression怎么用?C# ListInitExpression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ListInitExpression类属于System.Linq.Expressions命名空间,在下文中一共展示了ListInitExpression类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: typeof
string tree1 = "maple";
string tree2 = "oak";
System.Reflection.MethodInfo addMethod = typeof(Dictionary<int, string>).GetMethod("Add");
// Create two ElementInit objects that represent the
// two key-value pairs to add to the Dictionary.
System.Linq.Expressions.ElementInit elementInit1 =
System.Linq.Expressions.Expression.ElementInit(
addMethod,
System.Linq.Expressions.Expression.Constant(tree1.Length),
System.Linq.Expressions.Expression.Constant(tree1));
System.Linq.Expressions.ElementInit elementInit2 =
System.Linq.Expressions.Expression.ElementInit(
addMethod,
System.Linq.Expressions.Expression.Constant(tree2.Length),
System.Linq.Expressions.Expression.Constant(tree2));
// Create a NewExpression that represents constructing
// a new instance of Dictionary<int, string>.
System.Linq.Expressions.NewExpression newDictionaryExpression =
System.Linq.Expressions.Expression.New(typeof(Dictionary<int, string>));
// Create a ListInitExpression that represents initializing
// a new Dictionary<> instance with two key-value pairs.
System.Linq.Expressions.ListInitExpression listInitExpression =
System.Linq.Expressions.Expression.ListInit(
newDictionaryExpression,
elementInit1,
elementInit2);
Console.WriteLine(listInitExpression.ToString());
输出:
new Dictionary`2() {Void Add(Int32, System.String)(5,"maple"), Void Add(Int32, System.String)(3,"oak")}