本文整理汇总了C#中Expression.Cast方法的典型用法代码示例。如果您正苦于以下问题:C# Expression.Cast方法的具体用法?C# Expression.Cast怎么用?C# Expression.Cast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Expression
的用法示例。
在下文中一共展示了Expression.Cast方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Process
/////////////////////////////////////////////////////////////////////////////
public string Process( Expression exp )
{
// ******
string result = string.Empty;
// ******
switch( exp.NodeType ) {
case ExpressionType.Compound:
StringBuilder sb = new StringBuilder();
//foreach( Expression ex in exp.AsCompound().Items ) {
foreach( Expression ex in exp.Cast<CompoundExpression>().Items ) {
sb.Append( Process(ex) );
}
result = sb.ToString();
break;
case ExpressionType.MemberAccess:
result = DumpMemberAccessExpression( exp.Cast<MemberAccessExpression>() );
break;
case ExpressionType.UnnamedMemberAccess:
result = DumpMacroMemberAccessExpression( exp.Cast<UnnamedMemberAccessExpression>() );
break;
case ExpressionType.MethodCall:
result = DumpMethodCallExpression( exp.Cast<MethodCallExpression>() );
break;
//case ExpressionType.MacroMethodCall:
// result = DumpUnnamedMethodCallExpression( exp.Cast<UnnamedMethodCallExpression>() );
// break;
case ExpressionType.Index:
result = DumpIndexExpression( exp.Cast<IndexExpression>() );
break;
//case ExpressionType.MacroIndex:
// result = DumpUnnamedIndexExpression( exp.Cast<UnnamedIndexExpression>() );
// break;
case ExpressionType.Action:
//result = DumpActionExpression( exp.AsAction() );
result = DumpActionExpression( exp.Cast<ActionExpression>() );
break;
default:
throw ExceptionHelpers.CreateException( "unknown expression type \"{0}\"", exp.NodeType.ToString() );
}
// ******
return result;
}
示例2: GetArguments
/////////////////////////////////////////////////////////////////////////////
public static ArgumentList GetArguments( string macroName, Expression expIn, out int skipCount )
{
// ******
if( ExpressionType.Compound != expIn.NodeType ) {
throw new Exception( string.Format( "macro handler expeced a compound expression invoking macro \"{0}\"", macroName ) );
}
//
// arguments for the first item in the compound expression list
//
// ******
int usedExpressionCount = 0;
// ******
var expList = expIn.Cast<MacroExpression>().Items;
Expression exp = expList [ 0 ];
// ******
ArgumentList argList = null;
switch( exp.NodeType ) {
case ExpressionType.Index:
case ExpressionType.MethodCall:
case ExpressionType.UnnamedIndex:
case ExpressionType.UnnamedMethodCall:
//
// arguments to the indexer method, or the arguments to the
// method being called
//
argList = GetArgumentList( exp );
usedExpressionCount = 1;
break;
case ExpressionType.MemberAccess:
case ExpressionType.UnnamedMemberAccess:
//
// just the Unnamed invoking expression
//
/*
ok, what were looking at is
since a macro is being accessed (otherwise we would not be called) we
know this is an UnnamedMemberAccess, not MemberAccess
so:
a. UnnamedMemberAccess is the first expression
b. if there is an additional expression we check that expression
to see if its an index or method call expression: [] or ()
following the member access (e.g. macro[] or macro())
if it is (index or method call) then we return the arguments
that were included in that call - maybe some, maybe none
its up to our caller to to know what to do
in practice, we will only be called by block macros which are
??? always interpreted as method calls
*/
if( expList.Count > 1 ) {
exp = expList [ 1 ];
if( exp.IsIndexOrMethodCallExpression() ) {
argList = GetArgumentList( exp );
//
// we use the UnnmaedMemberAccess expression AND the index or
// method call expression that follows (as noted above)
//
usedExpressionCount = 2;
}
}
break;
//
// macros with the NonExpressive flag set will have a Null expression; the
// expression should be ignored
//
case ExpressionType.Null:
usedExpressionCount = 1;
break;
default:
throw new Exception( string.Format( "macro handler expeced an UnnamedMethodCall, UnnamedIndex or UnnamedMemberAccess Expression invoking macro \"{0}\"", macroName ) );
}
// ******
if( null == argList ) {
//throw ExceptionHelpers.CreateException( "macro handler expeced an argument list for UnnamedMethodCall, UnnmaedIndex or UnnamedMemberAccess Expression, invoking macro \"{0}\"", macroName );
argList = new ArgumentList();
}
//.........这里部分代码省略.........
示例3: Process
/////////////////////////////////////////////////////////////////////////////
protected object Process( object objIn, Expression exp )
{
// ******
if( null == objIn ) {
//
// this should ONLY happen when we're called by Arguments(...)
//
}
// ******
object obj;
// ******
switch( exp.NodeType ) {
case ExpressionType.Compound:
obj = EvalCompoundExpression( objIn, exp.Cast<CompoundExpression>() );
break;
case ExpressionType.MemberAccess:
obj = EvalMemberAccessExpression( objIn, exp.Cast<MemberAccessExpression>() );
break;
case ExpressionType.UnnamedMemberAccess:
obj = EvalUnnamedMemberAccessExpression( objIn, exp.Cast<UnnamedMemberAccessExpression>() );
break;
case ExpressionType.MethodCall:
obj = EvalMethodCallExpression( objIn, exp.Cast<MethodCallExpression>() );
break;
case ExpressionType.UnnamedMethodCall:
obj = EvalUnnamedMethodCallExpression( objIn, exp.Cast<UnnamedMethodCallExpression>() );
break;
case ExpressionType.Index:
obj = EvalIndexExpression( objIn, exp.Cast<IndexExpression>() );
break;
case ExpressionType.UnnamedIndex:
obj = EvalUnnamedIndexExpression( objIn, exp.Cast<UnnamedIndexExpression>() );
break;
case ExpressionType.Action:
//obj = EvalActionExpression( objIn, exp.AsAction() );
//break;
default:
throw ExceptionHelpers.CreateException( "unknown expression type \"{0}\"", exp.NodeType.ToString() );
}
// ******
return obj;
}