本文整理汇总了C#中DynamicMetaObject.Restrict方法的典型用法代码示例。如果您正苦于以下问题:C# DynamicMetaObject.Restrict方法的具体用法?C# DynamicMetaObject.Restrict怎么用?C# DynamicMetaObject.Restrict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DynamicMetaObject
的用法示例。
在下文中一共展示了DynamicMetaObject.Restrict方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MakeUnaryNotOperation
private static DynamicMetaObject/*!*/ MakeUnaryNotOperation(DynamicMetaObjectBinder/*!*/ operation, DynamicMetaObject/*!*/ self, Type retType, DynamicMetaObject errorSuggestion) {
self = self.Restrict(self.GetLimitType());
SlotOrFunction nonzero = SlotOrFunction.GetSlotOrFunction(PythonContext.GetPythonContext(operation), "__nonzero__", self);
SlotOrFunction length = SlotOrFunction.GetSlotOrFunction(PythonContext.GetPythonContext(operation), "__len__", self);
Expression notExpr;
if (!nonzero.Success && !length.Success) {
// no __len__ or __nonzero__, for None this is always false, everything else is True. If we have
// an error suggestion though we'll go with that.
if (errorSuggestion == null) {
notExpr = (self.GetLimitType() == typeof(DynamicNull)) ? AstUtils.Constant(true) : AstUtils.Constant(false);
} else {
notExpr = errorSuggestion.Expression;
}
} else {
SlotOrFunction target = nonzero.Success ? nonzero : length;
notExpr = target.Target.Expression;
if (nonzero.Success) {
// call non-zero and negate it
if (notExpr.Type == typeof(bool)) {
notExpr = Ast.Equal(notExpr, AstUtils.Constant(false));
} else {
notExpr = Ast.Call(
typeof(PythonOps).GetMethod("Not"),
AstUtils.Convert(notExpr, typeof(object))
);
}
} else {
// call len, compare w/ zero
if (notExpr.Type == typeof(int)) {
notExpr = Ast.Equal(notExpr, AstUtils.Constant(0));
} else {
notExpr =
Ast.Equal(
DynamicExpression.Dynamic(
PythonContext.GetPythonContext(operation).Operation(
PythonOperationKind.Compare
),
typeof(int),
notExpr,
AstUtils.Constant(0)
),
AstUtils.Constant(0)
);
}
}
}
if (retType == typeof(object) && notExpr.Type == typeof(bool)) {
notExpr = BindingHelpers.AddPythonBoxing(notExpr);
}
return new DynamicMetaObject(
notExpr,
self.Restrictions.Merge(nonzero.Target.Restrictions.Merge(length.Target.Restrictions))
);
}
示例2: MakeUnaryOperation
private static DynamicMetaObject MakeUnaryOperation(DynamicMetaObjectBinder binder, DynamicMetaObject self, string symbol, DynamicMetaObject errorSuggestion) {
self = self.Restrict(self.GetLimitType());
SlotOrFunction func = SlotOrFunction.GetSlotOrFunction(PythonContext.GetPythonContext(binder), symbol, self);
if (!func.Success) {
// we get the error message w/ {0} so that PythonBinderHelper.TypeError formats it correctly
return errorSuggestion ?? TypeError(binder, MakeUnaryOpErrorMessage(symbol, "{0}"), self);
}
return func.Target;
}
示例3: MakeEnumeratorOperation
private static DynamicMetaObject MakeEnumeratorOperation(PythonOperationBinder operation, DynamicMetaObject self) {
if (self.GetLimitType() == typeof(string)) {
self = self.Restrict(self.GetLimitType());
return new DynamicMetaObject(
Expression.Call(
typeof(PythonOps).GetMethod("StringEnumerator"),
self.Expression
),
self.Restrictions
);
} else if (self.GetLimitType() == typeof(Bytes)) {
self = self.Restrict(self.GetLimitType());
if (operation.Context.PythonOptions.Python30) {
return new DynamicMetaObject(
Expression.Call(
typeof(PythonOps).GetMethod("BytesIntEnumerator"),
self.Expression
),
self.Restrictions
);
} else {
return new DynamicMetaObject(
Expression.Call(
typeof(PythonOps).GetMethod("BytesEnumerator"),
self.Expression
),
self.Restrictions
);
}
} else if ((self.Value is IEnumerable ||
typeof(IEnumerable).IsAssignableFrom(self.GetLimitType())) && !(self.Value is PythonGenerator)) {
self = self.Restrict(self.GetLimitType());
return new DynamicMetaObject(
Expression.Call(
typeof(PythonOps).GetMethod("GetEnumeratorFromEnumerable"),
Expression.Convert(
self.Expression,
typeof(IEnumerable)
)
),
self.Restrictions
);
} else if (self.Value is IEnumerator || // check for COM object (and fast check when we have values)
typeof(IEnumerator).IsAssignableFrom(self.GetLimitType())) { // check if we don't have a value
DynamicMetaObject ieres = new DynamicMetaObject(
MakeEnumeratorResult(
Ast.Convert(
self.Expression,
typeof(IEnumerator)
)
),
self.Restrict(self.GetLimitType()).Restrictions
);
#if FEATURE_COM
if (Microsoft.Scripting.ComInterop.ComBinder.IsComObject(self.Value)) {
ieres = new DynamicMetaObject(
MakeEnumeratorResult(
Expression.Convert(
self.Expression,
typeof(IEnumerator)
)
),
ieres.Restrictions.Merge(
BindingRestrictions.GetExpressionRestriction(
Ast.TypeIs(self.Expression, typeof(IEnumerator))
)
)
);
}
#endif
return ieres;
}
ParameterExpression tmp = Ast.Parameter(typeof(IEnumerator), "enum");
IPythonConvertible pyConv = self as IPythonConvertible;
PythonConversionBinder convBinder = PythonContext.GetPythonContext(operation).Convert(typeof(IEnumerator), ConversionResultKind.ExplicitTry);
DynamicMetaObject res;
if (pyConv != null) {
res = pyConv.BindConvert(convBinder);
} else {
res = convBinder.Bind(self, new DynamicMetaObject[0]);
}
return new DynamicMetaObject(
Expression.Block(
new[] { tmp },
Ast.Condition(
Ast.NotEqual(
Ast.Assign(tmp, res.Expression),
AstUtils.Constant(null)
),
MakeEnumeratorResult(tmp),
Ast.Call(
//.........这里部分代码省略.........
示例4: MakeDictionaryCopy
/// <summary>
/// Called when the user is expanding a dictionary - we copy the user
/// dictionary and verify that it contains only valid string names.
/// </summary>
private DynamicMetaObject/*!*/ MakeDictionaryCopy(DynamicMetaObject/*!*/ userDict) {
Debug.Assert(_dict == null);
userDict = userDict.Restrict(userDict.GetLimitType());
_temps.Add(_dict = Ast.Variable(typeof(PythonDictionary), "$dict"));
EnsureInit();
string methodName;
if (typeof(PythonDictionary).IsAssignableFrom(userDict.GetLimitType())) {
methodName = "CopyAndVerifyPythonDictionary";
} else if (typeof(IDictionary).IsAssignableFrom(userDict.GetLimitType())) {
methodName = "CopyAndVerifyDictionary";
} else {
methodName = "CopyAndVerifyUserMapping";
}
_init.Add(
Ast.Assign(
_dict,
Ast.Call(
typeof(PythonOps).GetMethod(methodName),
GetFunctionParam(),
AstUtils.Convert(userDict.Expression, userDict.GetLimitType())
)
)
);
return userDict;
}
示例5: MakeHashOperation
private static DynamicMetaObject/*!*/ MakeHashOperation(PythonOperationBinder/*!*/ operation, DynamicMetaObject/*!*/ self) {
self = self.Restrict(self.GetLimitType());
PythonContext state = PythonContext.GetPythonContext(operation);
SlotOrFunction func = SlotOrFunction.GetSlotOrFunction(state, "__hash__", self);
DynamicMetaObject res = func.Target;
if (func.IsNull) {
// Python 2.6 setting __hash__ = None makes the type unhashable
res = new DynamicMetaObject(
operation.Throw(
Expression.Call(
typeof(PythonOps).GetMethod("TypeErrorForUnhashableObject"),
self.Expression
),
typeof(int)
),
res.Restrictions
);
} else if (func.ReturnType != typeof(int)) {
if (func.ReturnType == typeof(BigInteger)) {
// Python 2.5 defines the result of returning a long as hashing the long
res = new DynamicMetaObject(
HashBigInt(operation, res.Expression),
res.Restrictions
);
} else if (func.ReturnType == typeof(object)) {
// need to get the integer value here...
ParameterExpression tempVar = Ast.Parameter(typeof(object), "hashTemp");
res = new DynamicMetaObject(
Expression.Block(
new[] { tempVar },
Expression.Assign(tempVar, res.Expression),
Expression.Condition(
Expression.TypeIs(tempVar, typeof(int)),
Expression.Convert(tempVar, typeof(int)),
Expression.Condition(
Expression.TypeIs(tempVar, typeof(BigInteger)),
HashBigInt(operation, tempVar),
HashConvertToInt(state, tempVar)
)
)
),
res.Restrictions
);
} else {
// need to convert unknown value to object
res = new DynamicMetaObject(
HashConvertToInt(state, res.Expression),
res.Restrictions
);
}
}
return res;
}
示例6: MakeConvertRuleForCall
private DynamicMetaObject/*!*/ MakeConvertRuleForCall(DynamicMetaObjectBinder/*!*/ convertToAction, Type toType, DynamicMetaObject/*!*/ self, string name, string returner, Func<DynamicMetaObject> fallback, Func<Expression, Expression> resultConverter) {
PythonType pt = ((IPythonObject)self.Value).PythonType;
PythonTypeSlot pts;
CodeContext context = PythonContext.GetPythonContext(convertToAction).SharedContext;
ValidationInfo valInfo = BindingHelpers.GetValidationInfo(this, pt);
if (pt.TryResolveSlot(context, name, out pts) && !IsBuiltinConversion(context, pts, name, pt)) {
ParameterExpression tmp = Ast.Variable(typeof(object), "func");
Expression callExpr = resultConverter(
Ast.Call(
PythonOps.GetConversionHelper(returner, GetResultKind(convertToAction)),
Ast.Dynamic(
PythonContext.GetPythonContext(convertToAction).InvokeNone,
typeof(object),
PythonContext.GetCodeContext(convertToAction),
tmp
)
)
);
if (typeof(Extensible<>).MakeGenericType(toType).IsAssignableFrom(self.GetLimitType())) {
// if we're doing a conversion to the underlying type and we're an
// Extensible<T> of that type:
// if an extensible type returns it's self in a conversion, then we need
// to actually return the underlying value. If an extensible just keeps
// returning more instances of it's self a stack overflow occurs - both
// behaviors match CPython.
callExpr = AstUtils.Convert(AddExtensibleSelfCheck(convertToAction, toType, self, callExpr), typeof(object));
}
return BindingHelpers.AddDynamicTestAndDefer(
convertToAction,
new DynamicMetaObject(
Ast.Condition(
MakeTryGetTypeMember(
PythonContext.GetPythonContext(convertToAction),
pts,
self.Expression,
tmp
),
callExpr,
AstUtils.Convert(
ConversionFallback(convertToAction),
typeof(object)
)
),
self.Restrict(self.GetRuntimeType()).Restrictions
),
new DynamicMetaObject[] { this },
valInfo,
tmp
);
}
return fallback();
}
示例7: MakeUnaryOperation
private static DynamicMetaObject MakeUnaryOperation(DynamicMetaObjectBinder binder, DynamicMetaObject self, TotemOperationKind symbol, DynamicMetaObject errorSuggestion, Type retType)
{
self = self.Restrict(self.GetLimitType());
throw new NotImplementedException();
}
示例8: MakeEnumeratorOperation
private static DynamicMetaObject MakeEnumeratorOperation(PythonOperationBinder operation, DynamicMetaObject self) {
if (self.GetLimitType() == typeof(string)) {
self = self.Restrict(self.GetLimitType());
return new DynamicMetaObject(
Expression.Call(
typeof(PythonOps).GetMethod("StringEnumerator"),
self.Expression
),
self.Restrictions
);
} else if (self.GetLimitType() == typeof(PythonDictionary)) {
self = self.Restrict(self.GetLimitType());
return new DynamicMetaObject(
Expression.Call(
typeof(PythonOps).GetMethod("MakeDictionaryKeyEnumerator"),
self.Expression
),
self.Restrictions
);
} else if (self.Value is IEnumerable ||
typeof(IEnumerable).IsAssignableFrom(self.GetLimitType())) {
self = self.Restrict(self.GetLimitType());
return new DynamicMetaObject(
Expression.Call(
Expression.Convert(
self.Expression,
typeof(IEnumerable)
),
typeof(IEnumerable).GetMethod("GetEnumerator")
),
self.Restrictions
);
} else if (self.Value is IEnumerator || // check for COM object (and fast check when we have values)
typeof(IEnumerator).IsAssignableFrom(self.GetLimitType())) { // check if we don't have a value
DynamicMetaObject ieres = self.Restrict(self.GetLimitType());
#if !SILVERLIGHT
if (ComOps.IsComObject(self.Value)) {
ieres = new DynamicMetaObject(
self.Expression,
ieres.Restrictions.Merge(
BindingRestrictions.GetExpressionRestriction(
Ast.TypeIs(self.Expression, typeof(IEnumerator))
)
)
);
}
#endif
return ieres;
}
ParameterExpression tmp = Ast.Parameter(typeof(IEnumerator), "enum");
DynamicMetaObject res = self.BindConvert(new ConversionBinder(BinderState.GetBinderState(operation), typeof(IEnumerator), ConversionResultKind.ExplicitTry));
return new DynamicMetaObject(
Expression.Block(
new[] { tmp },
Ast.Condition(
Ast.NotEqual(
Ast.Assign(tmp, res.Expression),
AstUtils.Constant(null)
),
tmp,
Ast.Call(
typeof(PythonOps).GetMethod("ThrowTypeErrorForBadIteration"),
BinderState.GetCodeContext(operation),
self.Expression
)
)
),
res.Restrictions
);
}
示例9: MakeUnaryNotOperation
private static DynamicMetaObject/*!*/ MakeUnaryNotOperation(DynamicMetaObjectBinder/*!*/ operation, DynamicMetaObject/*!*/ self, Type/*!*/ retType) {
self = self.Restrict(self.GetLimitType());
SlotOrFunction nonzero = SlotOrFunction.GetSlotOrFunction(PythonContext.GetPythonContext(operation), Symbols.NonZero, self);
SlotOrFunction length = SlotOrFunction.GetSlotOrFunction(PythonContext.GetPythonContext(operation), Symbols.Length, self);
Expression notExpr;
if (!nonzero.Success && !length.Success) {
// always False or True for None
notExpr = (self.GetLimitType() == typeof(DynamicNull)) ? AstUtils.Constant(true) : AstUtils.Constant(false);
} else {
SlotOrFunction target = nonzero.Success ? nonzero : length;
notExpr = target.Target.Expression;
if (nonzero.Success) {
// call non-zero and negate it
if (notExpr.Type == typeof(bool)) {
notExpr = Ast.Equal(notExpr, AstUtils.Constant(false));
} else {
notExpr = Ast.Call(
typeof(PythonOps).GetMethod("Not"),
AstUtils.Convert(notExpr, typeof(object))
);
}
} else {
// call len, compare w/ zero
if (notExpr.Type == typeof(int)) {
notExpr = Ast.Equal(notExpr, AstUtils.Constant(0));
} else {
notExpr =
Ast.Equal(
Ast.Dynamic(
PythonContext.GetPythonContext(operation).Operation(
PythonOperationKind.Compare
),
typeof(int),
notExpr,
AstUtils.Constant(0)
),
AstUtils.Constant(0)
);
}
}
}
Debug.Assert(notExpr.Type == typeof(bool));
if (retType == typeof(object)) {
notExpr = BindingHelpers.AddPythonBoxing(notExpr);
}
return new DynamicMetaObject(
notExpr,
self.Restrictions.Merge(nonzero.Target.Restrictions.Merge(length.Target.Restrictions))
);
}
示例10: MakeHashOperation
private static DynamicMetaObject/*!*/ MakeHashOperation(PythonOperationBinder/*!*/ operation, DynamicMetaObject/*!*/ self) {
self = self.Restrict(self.GetLimitType());
BinderState state = BinderState.GetBinderState(operation);
SlotOrFunction func = SlotOrFunction.GetSlotOrFunction(state, Symbols.Hash, self);
DynamicMetaObject res = func.Target;
if (func.ReturnType != typeof(int)) {
if (func.ReturnType == typeof(BigInteger)) {
// Python 2.5 defines the result of returning a long as hashing the long
res = new DynamicMetaObject(
HashBigInt(operation, res.Expression),
res.Restrictions
);
} else if (func.ReturnType == typeof(object)) {
// need to get the integer value here...
ParameterExpression tempVar = Ast.Parameter(typeof(object), "hashTemp");
res = new DynamicMetaObject(
Expression.Block(
new [] { tempVar },
Expression.Assign(tempVar, res.Expression),
Expression.Condition(
Expression.TypeIs(tempVar, typeof(int)),
Expression.Convert(tempVar, typeof(int)),
Expression.Condition(
Expression.TypeIs(tempVar, typeof(BigInteger)),
HashBigInt(operation, tempVar),
HashConvertToInt(state, tempVar)
)
)
),
res.Restrictions
);
} else {
// need to convert unknown value to object
res = new DynamicMetaObject(
HashConvertToInt(state, res.Expression),
res.Restrictions
);
}
}
return res;
}
示例11: MakeDictionaryCopy
///-------------------------------------------------------------------------------------------------
/// <summary>
/// Called when the user is expanding a dictionary - we copy the user
/// dictionary and verify that it contains only valid string names.
/// </summary>
///
/// <remarks> Aleksander, 19.05.2013. </remarks>
///
/// <param name="userDict"> Dictionary of users. </param>
///
/// <returns> . </returns>
///-------------------------------------------------------------------------------------------------
private DynamicMetaObject MakeDictionaryCopy(DynamicMetaObject userDict)
{
Debug.Assert(_dict == null);
userDict = userDict.Restrict(userDict.GetLimitType());
_temps.Add(_dict = Expression.Variable(typeof(TotemDictionary), "$dict"));
EnsureInit();
MethodInfo method;
if (typeof(TotemDictionary).IsAssignableFrom(userDict.GetLimitType()))
method = AstMethods.CopyAndVerifyTotemDictionary;
else if (typeof(IDictionary).IsAssignableFrom(userDict.GetLimitType()))
method = AstMethods.CopyAndVerifyDictionary;
else
method = AstMethods.CopyAndVerifyUserMapping;
_init.Add(
Expression.Assign(
_dict,
Expression.Call(
method,
GetFunctionParam(),
Utils.Convert(userDict.Expression, userDict.GetLimitType())
)
)
);
return userDict;
}
示例12: MakeUnaryOperation
private static DynamicMetaObject/*!*/ MakeUnaryOperation(OperationBinder/*!*/ operation, DynamicMetaObject/*!*/ self) {
self = self.Restrict(self.GetLimitType());
SlotOrFunction func = SlotOrFunction.GetSlotOrFunction(BinderState.GetBinderState(operation), Symbols.OperatorToSymbol(operation.Operation), self);
if (!func.Success) {
// we get the error message w/ {0} so that PythonBinderHelper.TypeError formats it correctly
return TypeError(operation, MakeUnaryOpErrorMessage(operation.Operation.ToString(), "{0}"), self);
}
return func.Target;
}
示例13: MakeConvertRuleForCall
private DynamicMetaObject/*!*/ MakeConvertRuleForCall(ConvertBinder/*!*/ convertToAction, DynamicMetaObject/*!*/ self, SymbolId symbolId, string returner) {
PythonType pt = ((IPythonObject)self.Value).PythonType;
PythonTypeSlot pts;
CodeContext context = BinderState.GetBinderState(convertToAction).Context;
if (pt.TryResolveSlot(context, symbolId, out pts) && !IsBuiltinConversion(context, pts, symbolId, pt)) {
ParameterExpression tmp = Ast.Variable(typeof(object), "func");
Expression callExpr = Ast.Call(
PythonOps.GetConversionHelper(returner, GetResultKind(convertToAction)),
Ast.Dynamic(
new PythonInvokeBinder(
BinderState.GetBinderState(convertToAction),
new CallSignature(0)
),
typeof(object),
BinderState.GetCodeContext(convertToAction),
tmp
)
);
if (typeof(Extensible<>).MakeGenericType(convertToAction.Type).IsAssignableFrom(self.GetLimitType())) {
// if we're doing a conversion to the underlying type and we're an
// Extensible<T> of that type:
// if an extensible type returns it's self in a conversion, then we need
// to actually return the underlying value. If an extensible just keeps
// returning more instances of it's self a stack overflow occurs - both
// behaviors match CPython.
callExpr = AstUtils.Convert(AddExtensibleSelfCheck(convertToAction, self, callExpr), typeof(object));
}
return new DynamicMetaObject(
Ast.Block(
new ParameterExpression[] { tmp },
Ast.Condition(
BindingHelpers.CheckTypeVersion(
self.Expression,
pt.Version
),
Ast.Condition(
MakeTryGetTypeMember(
BinderState.GetBinderState(convertToAction),
pts,
self.Expression,
tmp
),
callExpr,
AstUtils.Convert(
ConversionFallback(convertToAction),
typeof(object)
)
),
convertToAction.Defer(this).Expression
)
),
self.Restrict(self.GetRuntimeType()).Restrictions
);
}
return convertToAction.FallbackConvert(this);
}
示例14: GenericCall
/// <summary>
/// Transforms a call into a Python GetMember/Invoke. This isn't quite the correct semantic as
/// we shouldn't be returning Python members (e.g. object.__repr__) to non-Python callers. This
/// can go away as soon as all of the classes implement the full fidelity of the protocol
/// </summary>
internal static DynamicMetaObject/*!*/ GenericCall(InvokeMemberBinder/*!*/ action, DynamicMetaObject target, DynamicMetaObject/*!*/[]/*!*/ args) {
if (target.NeedsDeferral()) {
return action.Defer(args);
}
return new DynamicMetaObject(
Invoke(
BinderState.GetCodeContext(action),
BinderState.GetBinderState(action),
typeof(object),
GetCallSignature(action),
ArrayUtils.Insert(
Binders.Get(
BinderState.GetCodeContext(action),
BinderState.GetBinderState(action),
typeof(object),
action.Name,
target.Expression
),
DynamicUtils.GetExpressions(args)
)
),
BindingRestrictions.Combine(args).Merge(target.Restrict(target.GetLimitType()).Restrictions)
);
}