本文整理汇总了C#中Engine.Throw方法的典型用法代码示例。如果您正苦于以下问题:C# Engine.Throw方法的具体用法?C# Engine.Throw怎么用?C# Engine.Throw使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Engine
的用法示例。
在下文中一共展示了Engine.Throw方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetField
public static object GetField(Engine engine, object context, string fieldName)
{
if (context == null) engine.Throw("context cannot be null for field: " + fieldName);
var fieldInfo = context.GetType().GetField(fieldName);
if (fieldInfo != null) return fieldInfo.GetValue(context);
return engine.Throw("no such field: " + fieldName);
}
示例2: CallMethod
public static object CallMethod(string methodName, bool staticMethod, Type typeToCall, object callee, IEnumerable<object> args, Type[] typeArgs, Engine engine)
{
var bindingFlags = (staticMethod ? BindingFlags.Static : BindingFlags.Instance) |
BindingFlags.Public | BindingFlags.FlattenHierarchy;
var methodInfo = null as MethodInfo;
if (typeArgs != null)
{
// Use type arguments to choose overload.
methodInfo = typeToCall.GetMethod(methodName, bindingFlags, null, typeArgs, null);
}
else
{
// Look for a matching method.
var candidates = typeToCall.GetMethods(bindingFlags).Where(info => info.Name == methodName);
var count = candidates.Count();
if (count >= 1)
{
if (count == 1)
methodInfo = candidates.First();
else
{
// Use arguments to choose overload.
var types = args.Select(value => value != null ? value.GetType() : typeof(object));
methodInfo = typeToCall.GetMethod(methodName, bindingFlags, null, types.ToArray(), null);
if (methodInfo == null) engine.Throw("method overload not found: " + methodName);
}
}
}
if (methodInfo == null) engine.Throw("method not found: " + methodName);
var parameters = methodInfo.GetParameters();
var methodArgs = null as object[];
if (HasParamsParameter(parameters))
{
var m = parameters.Length - 1;
var initiaArgs = ConvertArguments(parameters.Take(m), args.Take(m));
var paramsType = parameters[m].ParameterType.GetElementType();
var remainingArgs = TypeHelper.CreateArray(args.Skip(m).ToArray(), paramsType);
methodArgs = initiaArgs.Concat(new object[] { remainingArgs }).ToArray();
}
else
{
if (parameters.Length != args.Count()) engine.Throw("argument count mismatch: {0} != {1}", parameters.Length, args.Count());
methodArgs = ConvertArguments(parameters, args).ToArray();
}
bool trace = engine.ShouldTrace(TraceFlags.Call);
if (trace)
{
TraceCall("args", methodName, args, engine);
TraceCall("methodArgs", methodName, methodArgs, engine);
}
var result = methodInfo.Invoke(callee, methodArgs);
if (trace) engine.Trace(TraceFlags.Call, "Call {0} = {1}", methodName, result);
return result;
}
示例3: GetEnumValue
public static object GetEnumValue(Engine engine, Type type, string enumName)
{
if (!type.IsEnum) engine.Throw("not an enum: " + type.FullName);
try
{
return Enum.Parse(type, enumName, false);
}
catch
{
}
return engine.Throw("enum value not found: " + enumName);
}
示例4: OnExecute
protected override void OnExecute(Engine engine)
{
if (engine.HasBindingOrValue(ValueProperty, Path))
{
if ((bool)engine.Get(ValueProperty, Path, CodeTree, typeof(bool))) Body.Execute(engine);
return;
}
if (Body.Count == 0) engine.Throw("missing condition");
var expression = Body[0] as IExpression;
if (expression == null) engine.Throw("missing expression");
if (!TypeHelper.ConvertToBool(expression.Get(engine))) return;
Body.ExecuteSkipOne(engine);
}
示例5: GetProperty
public static object GetProperty(Engine engine, object context, string propertyName)
{
if (context == null) engine.Throw("context cannot be null for property: " + propertyName);
object value = null;
var propertyInfo = context.GetType().GetProperty(propertyName);
if (propertyInfo != null)
{
value = propertyInfo.GetValue(context, null);
return value;
}
#if !SILVERLIGHT
if (!Configuration.Silverlight)
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(context))
if (descriptor.Name == propertyName) return descriptor.GetValue(context);
#endif
if (context is System.Dynamic.DynamicObject)
if ((context as System.Dynamic.DynamicObject).TryGetMember(new BasicGetMemberBinder(propertyName), out value)) return value;
#if SILVERLIGHT
if (context is IDynamicObject)
{
var dynamicObject = context as IDynamicObject;
foreach (var property in dynamicObject.DynamicProperties)
if (property.Name == propertyName) return dynamicObject[propertyName];
}
#endif
return engine.Throw("no such property: " + propertyName);
}
示例6: ParseParameter
private Parameter ParseParameter(Engine engine, string parameter)
{
if (!parameter.Contains(' ')) return new Parameter { ParameterName = parameter};
var fields = parameter.Split(' ');
if (fields.Length != 2 || fields[0] != "params") engine.Throw("invalid params: " + parameter);
return new Parameter { ParameterName = fields[1], Params = true };
}
示例7: CallAccessor
public static object CallAccessor(Engine engine, bool isSet, object context, params object[] rawArgs)
{
if (context == null) engine.Throw("context cannot be null for item accessor");
var contextType = context.GetType();
var propertyInfo = contextType.GetProperty("Item");
if (propertyInfo == null && context is IList) propertyInfo = typeof(IList).GetProperty("Item");
if (propertyInfo == null) engine.Throw("no such property");
var methodInfo = isSet ? propertyInfo.GetSetMethod() : propertyInfo.GetGetMethod();
if (methodInfo == null) engine.Throw("no such method");
var parameters = methodInfo.GetParameters();
if (parameters.Length != rawArgs.Length)
engine.Throw("indexer count mismatch: {0} != {1}", parameters.Length, rawArgs.Length);
var args = parameters.Zip(rawArgs,
(parameter, indexer) => TypeHelper.Convert(indexer, parameter.ParameterType)).ToArray();
return methodInfo.Invoke(context, args);
}
示例8: OnActiveExecute
protected override void OnActiveExecute(Engine engine)
{
if (Path != null)
{
CodeTree.Compile(engine, CodeType.Event, Path);
var context = CodeTree.GetContext(engine);
if (context == null) engine.Throw("context cannot be null for event: " + Path);
RegisterHandler(engine, context, CodeTree.GetEvent(engine));
}
else
RegisterHandler(engine, null, null);
if (State == null) State = engine.GetClosure();
}
示例9: OnExecute
protected override void OnExecute(Engine engine)
{
if (Prototype != null)
{
var func = Prototype;
var open = func.IndexOf('(');
var close = func.LastIndexOf(')');
if (open == -1 || close == -1) engine.Throw("missing parentheses: " + func);
FunctionName = func.Substring(0, open);
var fields = func.Substring(open + 1, close - (open + 1)).Split(',');
Parameters.AddRange(fields.Select(field => ParseParameter(engine, field.Trim())));
}
engine.DefineFunction(FunctionName, this);
}
示例10: Call
public static object Call(string path, CodeTree codeTree, string staticMethodName, string methodName, string functionName, BuiltinFunction builtinFunction, Type type, ExpressionCollection typeArguments, IEnumerable<object> args, Engine engine)
{
if (path != null)
return engine.CallPath(path, codeTree, args);
if (functionName != null)
return engine.CallFunction(functionName, args);
if (builtinFunction != 0)
return engine.CallBuiltinFunction(builtinFunction, args);
var typeArgs = typeArguments.Count != 0 ? typeArguments.Get(engine).Cast<Type>().ToArray() : null;
if (staticMethodName != null)
return CallHelper.CallMethod(staticMethodName, true, type, null, args, typeArgs, engine);
if (methodName != null)
return CallHelper.CallMethod(methodName, false, type ?? engine.Context.GetType(), engine.Context, args, typeArgs, engine);
return engine.Throw("nothing to call");
}
示例11: OnGet
protected override object OnGet(Engine engine)
{
if (Expression == null) engine.Throw("path expression not specified");
return engine.GetPath(Expression, CodeTree);
}
示例12: EvaluateItems
private IList EvaluateItems(Engine engine, object value)
{
var result = engine.GetExpression(value);
if (!(result is IList)) engine.Throw("value does not evaluate to a collection");
return result as IList;
}
示例13: RegisterHandler
protected void RegisterHandler(Engine engine, object alternateContext, string alternateEventName)
{
var context = alternateContext ?? AssociatedObject;
registeredEventName = EventName ?? alternateEventName ?? AttachedKey;
if (registeredEventName == AttachedKey)
{
EventHandler(this, null);
return;
}
var eventInfo = context.GetType().GetEvent(registeredEventName);
if (eventInfo == null) engine.Throw("no such event: " + registeredEventName);
eventInfo.AddEventHandler(context,
Delegate.CreateDelegate(eventInfo.EventHandlerType, this, handlerMethodInfo));
}
示例14: OnGet
protected override object OnGet(Engine engine)
{
if (Op == default(Op)) engine.Throw("missing operator");
var type = engine.GetType(TypeProperty, TypePath, TypeCodeTree);
if (Arguments.Count == 0)
{
var arity = Op.GetArity();
if (arity == 1)
return engine.Operator(Op, engine.Get(ValueProperty, Path, CodeTree, type));
var value1 = engine.Get(Value1Property, Path1, CodeTree1, type);
var value2 = engine.Get(Value2Property, Path2, CodeTree2, type);
return engine.Operator(Op, value1, value2);
}
return engine.Operator(Op, Arguments);
}
示例15: GetPropertyType
public static Type GetPropertyType(Engine engine, object context, string propertyName)
{
if (context == null) engine.Throw("context cannot be null for property: " + propertyName);
var propertyInfo = context.GetType().GetProperty(propertyName);
if (propertyInfo != null) return propertyInfo.PropertyType;
#if !SILVERLIGHT
if (!Configuration.Silverlight)
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(context))
if (descriptor.Name == propertyName) return descriptor.PropertyType;
#endif
if (context is System.Dynamic.DynamicObject)
{
var value = GetProperty(engine, context, propertyName);
return value != null ? value.GetType() : typeof(object);
}
#if SILVERLIGHT
if (context is IDynamicObject)
{
var dynamicObject = context as IDynamicObject;
foreach (var property in dynamicObject.DynamicProperties)
if (property.Name == propertyName) return property.Type;
}
#endif
return engine.Throw("no such property: " + propertyName) as Type;
}