本文整理汇总了C#中Jurassic.Library.ObjectInstance.DefineProperty方法的典型用法代码示例。如果您正苦于以下问题:C# ObjectInstance.DefineProperty方法的具体用法?C# ObjectInstance.DefineProperty怎么用?C# ObjectInstance.DefineProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Jurassic.Library.ObjectInstance
的用法示例。
在下文中一共展示了ObjectInstance.DefineProperty方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PopulateMembers
// METHODS
//_________________________________________________________________________________________
/// <summary>
/// Populates the given object with properties, field and methods based on the given .NET
/// type.
/// </summary>
/// <param name="target"> The object to populate. </param>
/// <param name="type"> The .NET type to search for methods. </param>
/// <param name="flags"> <c>BindingFlags.Static</c> to populate static methods;
/// <c>BindingFlags.Instance</c> to populate instance methods. </param>
internal static void PopulateMembers(ObjectInstance target, Type type, BindingFlags flags)
{
// Register static methods as functions.
var methodGroups = new Dictionary<string, List<MethodBase>>();
foreach (var member in type.GetMembers(BindingFlags.Public | BindingFlags.DeclaredOnly | flags))
{
switch (member.MemberType)
{
case MemberTypes.Method:
MethodInfo method = (MethodInfo)member;
List<MethodBase> methodGroup;
if (methodGroups.TryGetValue(method.Name, out methodGroup) == true)
methodGroup.Add(method);
else
methodGroups.Add(method.Name, new List<MethodBase>() { method });
break;
case MemberTypes.Property:
PropertyInfo property = (PropertyInfo)member;
var getMethod = property.GetGetMethod();
ClrFunction getter = getMethod == null ? null : new ClrFunction(target.Engine.Function.InstancePrototype, new ClrBinder(getMethod));
var setMethod = property.GetSetMethod();
ClrFunction setter = setMethod == null ? null : new ClrFunction(target.Engine.Function.InstancePrototype, new ClrBinder(setMethod));
target.DefineProperty(property.Name, new PropertyDescriptor(getter, setter, PropertyAttributes.NonEnumerable), false);
// Property getters and setters also show up as methods, so remove them here.
// NOTE: only works if properties are enumerated after methods.
if (getMethod != null)
methodGroups.Remove(getMethod.Name);
if (setMethod != null)
methodGroups.Remove(setMethod.Name);
break;
case MemberTypes.Field:
FieldInfo field = (FieldInfo)member;
ClrFunction fieldGetter = new ClrFunction(target.Engine.Function.InstancePrototype, new FieldGetterBinder(field));
ClrFunction fieldSetter = new ClrFunction(target.Engine.Function.InstancePrototype, new FieldSetterBinder(field));
target.DefineProperty(field.Name, new PropertyDescriptor(fieldGetter, fieldSetter, PropertyAttributes.NonEnumerable), false);
break;
case MemberTypes.Constructor:
case MemberTypes.NestedType:
case MemberTypes.Event:
case MemberTypes.TypeInfo:
// Support not yet implemented.
break;
}
}
foreach (var methodGroup in methodGroups.Values)
{
var binder = new ClrBinder(methodGroup);
var function = new ClrFunction(target.Engine.Function.InstancePrototype, binder);
target.FastSetProperty(binder.Name, function, PropertyAttributes.NonEnumerable, overwriteAttributes: true);
}
}
示例2: SetObjectLiteralValue
// CODE GEN METHODS
//_________________________________________________________________________________________
/// <summary>
/// Sets the value of a object literal property to a value.
/// </summary>
/// <param name="obj"> The object to set the property on. </param>
/// <param name="key"> The property key (can be a string or a symbol). </param>
/// <param name="value"> The value to set. </param>
public static void SetObjectLiteralValue(ObjectInstance obj, object key, object value)
{
obj.DefineProperty(key, new PropertyDescriptor(value, Library.PropertyAttributes.FullAccess), throwOnError: false);
}
示例3: DefineProperty
public static ObjectInstance DefineProperty(ObjectInstance obj, object key, object attributes)
{
key = TypeConverter.ToPropertyKey(key);
var defaults = obj.GetOwnPropertyDescriptor(key);
if (!(attributes is ObjectInstance))
throw new JavaScriptException(obj.Engine, ErrorType.TypeError, "Invalid descriptor for property '{propertyName}'.");
var descriptor = PropertyDescriptor.FromObject((ObjectInstance)attributes, defaults);
obj.DefineProperty(key, descriptor, true);
return obj;
}
示例4: SetObjectLiteralSetter
/// <summary>
/// Sets the value of a object literal property to a setter. If the value already has a
/// getter then it will be retained.
/// </summary>
/// <param name="obj"> The object to set the property on. </param>
/// <param name="key"> The property key (can be a string or a symbol).</param>
/// <param name="setter"> The setter function. </param>
public static void SetObjectLiteralSetter(ObjectInstance obj, object key, UserDefinedFunction setter)
{
var descriptor = obj.GetOwnPropertyDescriptor(key);
if (descriptor.Exists == false || !descriptor.IsAccessor)
obj.DefineProperty(key, new PropertyDescriptor(null, setter, Library.PropertyAttributes.FullAccess), throwOnError: false);
else
obj.DefineProperty(key, new PropertyDescriptor(descriptor.Getter, setter, Library.PropertyAttributes.FullAccess), throwOnError: false);
}