本文整理汇总了C#中RubyContext.GetImmediateClassOf方法的典型用法代码示例。如果您正苦于以下问题:C# RubyContext.GetImmediateClassOf方法的具体用法?C# RubyContext.GetImmediateClassOf怎么用?C# RubyContext.GetImmediateClassOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RubyContext
的用法示例。
在下文中一共展示了RubyContext.GetImmediateClassOf方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryBind
public static DynamicMetaObject TryBind(RubyContext/*!*/ context, GetMemberBinder/*!*/ binder, DynamicMetaObject/*!*/ target) {
Assert.NotNull(context, target);
var metaBuilder = new MetaObjectBuilder();
var contextExpression = AstUtils.Constant(context);
RubyClass targetClass = context.GetImmediateClassOf(target.Value);
MethodResolutionResult method;
RubyMemberInfo methodMissing = null;
using (targetClass.Context.ClassHierarchyLocker()) {
metaBuilder.AddTargetTypeTest(target.Value, targetClass, target.Expression, context, contextExpression);
method = targetClass.ResolveMethodForSiteNoLock(binder.Name, RubyClass.IgnoreVisibility);
if (method.Found) {
methodMissing = targetClass.ResolveMethodForSiteNoLock(Symbols.MethodMissing, RubyClass.IgnoreVisibility).Info;
}
}
if (method.Found) {
// we need to create a bound member:
metaBuilder.Result = AstUtils.Constant(new RubyMethod(target.Value, method.Info, binder.Name));
} else {
// TODO:
// We need to throw an exception if we don't find method_missing so that our version update optimization works:
// This limits interop with other languages.
//
// class B CLR type with method 'foo'
// class C < B Ruby class
// x = C.new
//
// 1. x.GET("foo") from Ruby
// No method found or CLR method found -> fallback to Python
// Python might see its method foo or might just fallback to .NET,
// in any case it will add rule [1] with restriction on type of C w/o Ruby version check.
// 2. B.define_method("foo")
// This doesn't update C due to the optimization (there is no overridden method foo in C).
// 3. x.GET("foo") from Ruby
// This will not invoke the binder since the rule [1] is still valid.
//
object symbol = SymbolTable.StringToId(binder.Name);
RubyCallAction.BindToMethodMissing(metaBuilder,
new CallArguments(
new DynamicMetaObject(contextExpression, BindingRestrictions.Empty, context),
new[] {
target,
new DynamicMetaObject(AstUtils.Constant(symbol), BindingRestrictions.Empty, symbol)
},
RubyCallSignature.Simple(1)
),
binder.Name,
methodMissing,
method.IncompatibleVisibility,
false
);
}
// TODO: we should return null if we fail, we need to throw exception for now:
return metaBuilder.CreateMetaObject(binder, DynamicMetaObject.EmptyMetaObjects);
}
示例2: GetMethods
private static RubyArray/*!*/ GetMethods(RubyContext/*!*/ context, object self, bool inherited, RubyMethodAttributes attributes) {
RubyClass immediateClass = context.GetImmediateClassOf(self);
return ModuleOps.GetMethods(immediateClass, inherited, attributes);
}
示例3: GetSingletonMethods
public static RubyArray/*!*/ GetSingletonMethods(RubyContext/*!*/ context, object self, [DefaultParameterValue(true)]bool inherited) {
RubyClass immediateClass = context.GetImmediateClassOf(self);
return ModuleOps.GetMethods(immediateClass, inherited, RubyMethodAttributes.Singleton | RubyMethodAttributes.Public | RubyMethodAttributes.Protected);
}
示例4: GetMethods
public static RubyArray GetMethods(RubyContext/*!*/ context, object self, [DefaultParameterValue(true)]bool inherited)
{
var foreignMembers = context.GetForeignDynamicMemberNames(self);
RubyClass immediateClass = context.GetImmediateClassOf(self);
if (!inherited && !immediateClass.IsSingletonClass) {
var result = new RubyArray();
if (foreignMembers.Count > 0) {
foreach (var name in foreignMembers) {
if (Tokenizer.IsMethodName(name) || Tokenizer.IsOperatorName(name)) {
result.Add(new ClrName(name));
}
}
}
return result;
}
return ModuleOps.GetMethods(immediateClass, inherited, RubyMethodAttributes.Public | RubyMethodAttributes.Protected, foreignMembers);
}
示例5: AddTargetTypeTest
// TODO: do not test runtime for runtime bound sites
// TODO: ResolveMethod invalidates modules that were not initialized yet -> snapshot version after method resolution
// TODO: thread safety: synchronize version snapshot and method resolution
public void AddTargetTypeTest(object target, Expression/*!*/ targetParameter, RubyContext/*!*/ context, Expression/*!*/ contextExpression) {
// singleton nil:
if (target == null) {
AddRestriction(Ast.Equal(targetParameter, Ast.Constant(null)));
AddFullVersionTest(context.NilClass, context, contextExpression);
return;
}
// singletons true, false:
if (target is bool) {
AddRestriction(Ast.AndAlso(
Ast.TypeIs(targetParameter, typeof(bool)),
Ast.Equal(Ast.Convert(targetParameter, typeof(bool)), Ast.Constant(target))
));
if ((bool)target) {
AddFullVersionTest(context.TrueClass, context, contextExpression);
} else {
AddFullVersionTest(context.FalseClass, context, contextExpression);
}
return;
}
RubyClass immediateClass = context.GetImmediateClassOf(target);
// user defined instance singletons, modules, classes:
if (immediateClass.IsSingletonClass) {
AddRestriction(
Ast.Equal(
Ast.Convert(targetParameter, typeof(object)),
Ast.Convert(Ast.Constant(target), typeof(object))
)
);
// we need to check for a runtime (e.g. "foo" .NET string instance could be shared accross runtimes):
AddFullVersionTest(immediateClass, context, contextExpression);
return;
}
Type type = target.GetType();
AddTypeRestriction(type, targetParameter);
if (typeof(IRubyObject).IsAssignableFrom(type)) {
// Ruby objects (get the method directly to prevent interface dispatch):
MethodInfo classGetter = type.GetMethod("get_" + RubyObject.ClassPropertyName, BindingFlags.Public | BindingFlags.Instance);
if (classGetter != null && classGetter.ReturnType == typeof(RubyClass)) {
AddCondition(
// (#{type})target.Class.Version == #{immediateClass.Version}
Ast.Equal(
Ast.Call(Ast.Call(Ast.Convert(targetParameter, type), classGetter), RubyModule.VersionProperty.GetGetMethod()),
Ast.Constant(immediateClass.Version)
)
);
return;
}
// TODO: explicit iface-implementation
throw new NotSupportedException("Type implementing IRubyObject should have RubyClass getter");
} else {
// CLR objects:
AddFullVersionTest(immediateClass, context, contextExpression);
}
}