- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试创建一种方法(在运行时)为所有类型的委托(delegate)创建包装器。这是为了创建一种灵活的方式来注入(inject)额外的日志记录(在本例中)。在这第一步中,我尝试围绕给定的 input
创建一个 try-catch 环绕-参数。
try
{
Console.WriteLine(....);
// Here the original call
Console.WriteLine(....);
}
catch(Exception ex)
{
Console.WriteLine(.....);
}
我正在使用通用方法调用 CreateWrapper2
(见下文)
private static readonly MethodInfo ConsoleWriteLine = typeof(Console).GetMethod("WriteLine", new[] { typeof(string), typeof(object[]) });
private static MethodCallExpression WriteLinExpression(string format, params object[] args)
{
Expression[] expressionArguments = new Expression[2];
expressionArguments[0] = Expression.Constant(format, typeof(string));
expressionArguments[1] = Expression.Constant(args, typeof(object[]));
return Expression.Call(ConsoleWriteLine, expressionArguments);
}
public T CreateWrapper2<T>(T input)
{
Type type = typeof(T);
if (!typeof(Delegate).IsAssignableFrom(type))
{
return input;
}
PropertyInfo methodProperty = type.GetProperty("Method");
MethodInfo inputMethod = methodProperty != null ? (MethodInfo)methodProperty.GetValue(input) : null;
if (inputMethod == null)
{
return input;
}
string methodName = inputMethod.Name;
ParameterInfo[] parameters = inputMethod.GetParameters();
ParameterExpression[] parameterExpressions = new ParameterExpression[parameters.Length];
// TODO: Validate/test parameters, by-ref /out with attributes etc.
for (int idx = 0; idx < parameters.Length; idx++)
{
ParameterInfo parameter = parameters[idx];
parameterExpressions[idx] = Expression.Parameter(parameter.ParameterType, parameter.Name);
}
bool handleReturnValue = inputMethod.ReturnType != typeof(void);
ParameterExpression variableExpression = handleReturnValue ? Expression.Variable(inputMethod.ReturnType) : null;
MethodCallExpression start = WriteLinExpression("Starting '{0}'.", methodName);
MethodCallExpression completed = WriteLinExpression("Completed '{0}'.", methodName);
MethodCallExpression failed = WriteLinExpression("Failed '{0}'.", methodName);
Expression innerCall = Expression.Call(inputMethod, parameterExpressions);
LabelTarget returnTarget = Expression.Label(inputMethod.ReturnType);
LabelExpression returnLabel = Expression.Label(returnTarget, Expression.Default(returnTarget.Type)); ;
GotoExpression returnExpression = null;
if (inputMethod.ReturnType != typeof(void))
{
// Handle return value.
innerCall = Expression.Assign(variableExpression, innerCall);
returnExpression = Expression.Return(returnTarget, variableExpression, returnTarget.Type);
}
else
{
returnExpression = Expression.Return(returnTarget);
}
List<Expression> tryBodyElements = new List<Expression>();
tryBodyElements.Add(start);
tryBodyElements.Add(innerCall);
tryBodyElements.Add(completed);
if (returnExpression != null)
{
tryBodyElements.Add(returnExpression);
}
BlockExpression tryBody = Expression.Block(tryBodyElements);
BlockExpression catchBody = Expression.Block(tryBody.Type, new Expression[] { failed, Expression.Rethrow(tryBody.Type) });
CatchBlock catchBlock = Expression.Catch(typeof(Exception), catchBody);
TryExpression tryBlock = Expression.TryCatch(tryBody, catchBlock);
List<Expression> methodBodyElements = new List<Expression>();
if(variableExpression != null) methodBodyElements.Add(variableExpression);
methodBodyElements.Add(tryBlock);
methodBodyElements.Add(returnLabel);
Expression<T> wrapperLambda = Expression<T>.Lambda<T>(Expression.Block(methodBodyElements), parameterExpressions);
Console.WriteLine("lambda:");
Console.WriteLine(wrapperLambda.GetDebugView());
return wrapperLambda.Compile();
}
对于 void 方法(如 Action<>
),这段代码可以满足我的需要。但是当有返回值时,我得到异常“从范围''引用的'System.Boolean'类型的变量'',但它未定义”
许多其他帖子都在谈论Expression.Parameter
多次调用一个参数;对我来说,这里似乎有其他问题,但我找不到。一切顺利,直到 .Compile
线,在那里它崩溃了。
对于 Func<int, bool> target = i => i % 2 ==0;
下面是生成的表达式的 DebugView。
.Lambda #Lambda1<System.Func`2[System.Int32,System.Boolean]>(System.Int32 $i) {
.Block() {
$var1;
.Try {
.Block() {
.Call System.Console.WriteLine(
"Starting '{0}'.",
.Constant<System.Object[]>(System.Object[]));
$var1 = .Call LDAP.LdapProgram.<Main>b__0($i);
.Call System.Console.WriteLine(
"Completed '{0}'.",
.Constant<System.Object[]>(System.Object[]));
.Return #Label1 { $var1 }
}
} .Catch (System.Exception) {
.Block() {
.Call System.Console.WriteLine(
"Failed '{0}'.",
.Constant<System.Object[]>(System.Object[]));
.Rethrow
}
};
.Label
.Default(System.Boolean)
.LabelTarget #Label1:
}
}
我错过了什么?(在调试期间我试过:
Expression.Variable
从 try-body 内部到顶层。Expression.Return
为 catch block 提供与 try block 相同的 Body.Type .)
最佳答案
看起来您没有为 block 语句指定变量。
在错误中你正在创建一个参数并且没有给它一个名字,如果你这样做你会看到:
"variable 'varName' of type 'System.Boolean' referenced from scope 'varName', but it is not defined"
因此,为了将来引用,如果您在制作表达式树时给您的 vars 命名,那么以下内容应该可以工作,这会让您的生活变得更轻松
// Define the variable at the top of the block
// when we are returning something
if (variableExpression != null)
{
block = Expression.Block(new[] { variableExpression }, methodBodyElements);
}
else
{
block = Expression.Block(methodBodyElements);
}
Expression<T> wrapperLambda = Expression<T>.Lambda<T>(block, parameterExpressions);
return wrapperLambda.Compile();
关于c# - "variable ' ' of type ' System.Boolean ' referenced from scope ' ', but it is not defined"在表达式中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34356639/
我在这里有我的 javascript 代码: define(['controllers/controllers', 'services/alerts'], function(module) {
的意义是什么scope = scope-token *( SP scope-token ) scope-token = 1*( %x21 / %x23-5B / %x5D-7E ) 在 RFC6749
我是 AngularJS 的新手。我试图找出这两个 Controller 定义之间的区别: app.controller('simpleController', ['$scope', function
似乎所有 Guice 的开箱即用 Scope 实现本质上都是基于线程的(或完全忽略线程): Scopes.SINGLETON和 Scopes.NO_SCOPE忽略线程并且是边缘情况:全局范围和无范围。
如果这个问题涉及的是一个常见问题,我很抱歉,但我发现这个问题非常抽象,并且无法真正为其构建一个好的 Google 搜索词。 我试图理解并找到 Maven 中提供的依赖项的用例。我的想法是这样的: 假设
假设我有以下 Controller angular.module('scopeExample', []) .controller('MyController', ['$scope', func
当前在TmThemeEditor上注册的243种配色方案中, 我注意到几乎没有人使用范围选择器运算符。 对于以下情况,运算符非常有用: (text.html | text.xml) & (meta.t
我有一些行为不符合预期的代码......我在 AngularJS Controller 中有一个事件监听器,如下所示: $scope.$on("newClipSelected", function(e
首先,如果帖子太长,我深表歉意。另外,为了以防万一这会以某种方式干扰您可能给我的答案,我不会以通常的方式定义我的 Controller 。相反,我关注http://www.technofattie.c
我有一个模式,其中许多项目类型都是“可编辑的”。这意味着我有很多模板(每种可编辑项目类型一个),这些模板期望具有唯一的字段,但具有通用功能(编辑、保存、取消编辑、删除等)。这些常见功能导致 Contr
$evalAsync 和 $applyAsync 之间有什么区别?我的理解是,当我从指令中使用 $evalAsync 时,表达式将在浏览器呈现之前进行计算。 举个例子,如果我想滚动到页面上的特定位置但
我试图为一个 $scope 变量提供另一个 $scope 变量的值。有人能告诉我出了什么问题吗?查看简单的 plunker 了解详细信息: http://plnkr.co/edit/TlKnd2fM5
我有以下一段 Angular 代码 $scope.prepare = function(){ $scope.elems = [1,2,3]; }; $scope.action = functio
我正在关注 Angularjs 的官方教程,但我陷入了第 2 步。 这是一个片段,我不明白 $scope:scope 的含义, describe('PhoneListCtrl', function()
根据文档, Global: Component is shared among all users. Session: Separate instances of the component are
显示作用域变量,类似于 Angularjs 中的 ng-repeat 元素 这些是我的范围变量 $scope.published = true; $scope.count = 3; 我还有一个名为 l
我是 Angular 的新手,我想在普通的 javascript 中做一些非常简单的事情,但我无法找到如何在 Angular 中正确地做到这一点! 我想设置一个通用函数来清除输入文本字段: 我有这个
在article中发现了这样一个idea : Notice how the value function takes the scope as parameter (without the $ in
注释部分将位于 $scope.$on 下。我需要将 options 返回到我保存 $scope.$emit 的地方。请帮助!!! if (gridConfig.Batch) {
我有一个带有 2 个作用域的 Controller : app.controller('search', function($scope) { $scope.value1 = '';
我是一名优秀的程序员,十分优秀!