当前位置: 首页>>代码示例>>C#>>正文


C# Expression.Cast方法代码示例

本文整理汇总了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;
		}
开发者ID:jmclain,项目名称:Nmp,代码行数:56,代码来源:ExpressionDumper.cs

示例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();
			}
//.........这里部分代码省略.........
开发者ID:jmclain,项目名称:Nmp,代码行数:101,代码来源:BuiltinMacroHandler.cs

示例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;
		}
开发者ID:jmclain,项目名称:Nmp,代码行数:57,代码来源:ExpressionEvaluatorBase.cs


注:本文中的Expression.Cast方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。