本文整理汇总了C#中CompilationContext.EmitNewObject方法的典型用法代码示例。如果您正苦于以下问题:C# CompilationContext.EmitNewObject方法的具体用法?C# CompilationContext.EmitNewObject怎么用?C# CompilationContext.EmitNewObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CompilationContext
的用法示例。
在下文中一共展示了CompilationContext.EmitNewObject方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Compile
public void Compile(CompilationContext context)
{
if (ReflectionUtils.IsNullable(objectType))
{
IAstRefOrValue underlyingValue;
var underlyingType = Nullable.GetUnderlyingType(objectType);
if (ConstructorParams == null || ConstructorParams.Length == 0)
{
LocalBuilder temp = context.ilGenerator.DeclareLocal(underlyingType);
new AstInitializeLocalVariable(temp).Compile(context);
underlyingValue = AstBuildHelper.ReadLocalRV(temp);
}
else
{
underlyingValue = (IAstValue)ConstructorParams[0];
}
ConstructorInfo constructor = objectType.GetConstructor(
BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance,
null,
new[] { underlyingType },
null);
underlyingValue.Compile(context);
context.EmitNewObject(constructor);
}
else
{
Type[] types;
if (ConstructorParams == null || ConstructorParams.Length == 0)
{
types = new Type[0];
}
else
{
types = ConstructorParams.Select(c => c.itemType).ToArray();
foreach (var p in ConstructorParams)
{
p.Compile(context);
}
}
ConstructorInfo ci = objectType.GetConstructor(types);
if (ci != null)
{
context.EmitNewObject(ci);
}
else if (objectType.IsValueType)
{
LocalBuilder temp = context.ilGenerator.DeclareLocal(objectType);
new AstInitializeLocalVariable(temp).Compile(context);
AstBuildHelper.ReadLocalRV(temp).Compile(context);
}
else
{
throw new Exception(
String.Format("Constructor for types [{0}] not found in {1}", types.ToCSV(","), objectType.FullName)
);
}
}
}