本文整理汇总了C#中ObjectCreateExpression.ReplaceWith方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectCreateExpression.ReplaceWith方法的具体用法?C# ObjectCreateExpression.ReplaceWith怎么用?C# ObjectCreateExpression.ReplaceWith使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ObjectCreateExpression
的用法示例。
在下文中一共展示了ObjectCreateExpression.ReplaceWith方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitObjectCreateExpression
public override void VisitObjectCreateExpression (ObjectCreateExpression objectCreateExpression)
{
base.VisitObjectCreateExpression (objectCreateExpression);
var td = GetTypeDef (objectCreateExpression.Type);
if (td == null || !IsDelegate (td) || objectCreateExpression.Arguments.Count != 1)
return;
var a = objectCreateExpression.Arguments.First ();
a.Remove ();
objectCreateExpression.ReplaceWith (a);
}
示例2: HandleAnonymousMethod
bool HandleAnonymousMethod(ObjectCreateExpression objectCreateExpression, Expression target, MethodReference methodRef)
{
// Anonymous methods are defined in the same assembly, so there's no need to Resolve().
MethodDefinition method = methodRef as MethodDefinition;
if (method == null || !method.Name.StartsWith("<", StringComparison.Ordinal))
return false;
if (!(method.IsCompilerGenerated() || IsPotentialClosure(method.DeclaringType)))
return false;
// Decompile the anonymous method:
DecompilerContext subContext = context.Clone();
subContext.CurrentMethod = method;
BlockStatement body = AstMethodBodyBuilder.CreateMethodBody(method, subContext);
TransformationPipeline.RunTransformationsUntil(body, v => v is DelegateConstruction, subContext);
body.AcceptVisitor(this, null);
AnonymousMethodExpression ame = new AnonymousMethodExpression();
bool isLambda = false;
if (method.Parameters.All(p => string.IsNullOrEmpty(p.Name))) {
ame.HasParameterList = false;
} else {
ame.HasParameterList = true;
ame.Parameters.AddRange(AstBuilder.MakeParameters(method.Parameters));
if (ame.Parameters.All(p => p.ParameterModifier == ParameterModifier.None)) {
isLambda = (body.Statements.Count == 1 && body.Statements.Single() is ReturnStatement);
}
}
// Replace all occurrences of 'this' in the method body with the delegate's target:
foreach (AstNode node in body.Descendants) {
if (node is ThisReferenceExpression)
node.ReplaceWith(target.Clone());
}
if (isLambda) {
LambdaExpression lambda = new LambdaExpression();
ame.Parameters.MoveTo(lambda.Parameters);
Expression returnExpr = ((ReturnStatement)body.Statements.Single()).Expression;
returnExpr.Remove();
lambda.Body = returnExpr;
objectCreateExpression.ReplaceWith(lambda);
} else {
ame.Body = body;
objectCreateExpression.ReplaceWith(ame);
}
return true;
}