public static object Create(object o) { if (o is double) { o = new NumberProxy((double)o); } return(o); }
/// <summary> /// Provides the implementation for operations that get member values. Classes /// derived from the System.Dynamic.DynamicObject class can override this method /// to specify dynamic behavior for operations such as getting a value for a /// property. /// </summary> /// <param name="binder">Provides information about the object that called the dynamic operation. /// The binder.Name property provides the name of the member on which the dynamic /// operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) /// statement, where sampleObject is an instance of the class derived from the /// System.Dynamic.DynamicObject class, binder.Name returns "SampleProperty". /// The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param> /// <param name="result">The result of the get operation. For example, if the method is called for /// a property, you can assign the property value to result.</param> /// <returns>true if the operation is successful; otherwise, false. If this method returns /// false, the run-time binder of the language determines the behavior. (In most /// cases, a run-time exception is thrown.)</returns> public override bool TryGetMember(GetMemberBinder binder, out object result) { ILuaValue o; lock (this) o = GlobalsTable.GetItemRaw(_runtime.CreateValue(binder.Name)); MethodInfo asMethod = typeof(ILuaValue).GetMethod(nameof(ILuaValue.As)).MakeGenericMethod(binder.ReturnType); result = asMethod.Invoke(o, null); if (result is double) { result = new NumberProxy((double)result); } return(true); }
/// <summary> /// Provides the implementation for operations that get a value by index. Classes /// derived from the System.Dynamic.DynamicObject class can override this method /// to specify dynamic behavior for indexing operations. /// </summary> /// <param name="binder">Provides information about the operation.</param> /// <param name="indexes">The indexes that are used in the operation. For example, for the sampleObject[3] /// operation in C# (sampleObject(3) in Visual Basic), where sampleObject is /// derived from the DynamicObject class, indexes[0] is equal to 3.</param> /// <param name="result">The result of the index operation.</param> /// <returns>true if the operation is successful; otherwise, false. If this method returns /// false, the run-time binder of the language determines the behavior. (In most /// cases, a run-time exception is thrown.)</returns> public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { if (indexes != null && indexes.Length == 1) { ILuaValue o; lock (this) o = GlobalsTable.GetItemRaw(_runtime.CreateValue(indexes[0])); MethodInfo asMethod = typeof(ILuaValue).GetMethod(nameof(ILuaValue.As)).MakeGenericMethod(binder.ReturnType); result = asMethod.Invoke(o, null); if (result is double) { result = new NumberProxy((double)result); } return(true); } else { return(base.TryGetIndex(binder, indexes, out result)); } }
public static object Create(object o) { if (o is double) o = new NumberProxy((double)o); return o; }
/// <summary> /// Provides the implementation for operations that get member values. Classes /// derived from the System.Dynamic.DynamicObject class can override this method /// to specify dynamic behavior for operations such as getting a value for a /// property. /// </summary> /// <param name="binder">Provides information about the object that called the dynamic operation. /// The binder.Name property provides the name of the member on which the dynamic /// operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) /// statement, where sampleObject is an instance of the class derived from the /// System.Dynamic.DynamicObject class, binder.Name returns "SampleProperty". /// The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param> /// <param name="result">The result of the get operation. For example, if the method is called for /// a property, you can assign the property value to result.</param> /// <returns>true if the operation is successful; otherwise, false. If this method returns /// false, the run-time binder of the language determines the behavior. (In most /// cases, a run-time exception is thrown.)</returns> public override bool TryGetMember(GetMemberBinder binder, out object result) { ILuaValue o; lock (this) o = GlobalsTable.GetItemRaw(_runtime.CreateValue(binder.Name)); MethodInfo asMethod = typeof(ILuaValue).GetMethod("As").MakeGenericMethod(binder.ReturnType); result = asMethod.Invoke(o, null); if (result is double) result = new NumberProxy((double)result); return true; }
/// <summary> /// Provides the implementation for operations that get a value by index. Classes /// derived from the System.Dynamic.DynamicObject class can override this method /// to specify dynamic behavior for indexing operations. /// </summary> /// <param name="binder">Provides information about the operation.</param> /// <param name="indexes">The indexes that are used in the operation. For example, for the sampleObject[3] /// operation in C# (sampleObject(3) in Visual Basic), where sampleObject is /// derived from the DynamicObject class, indexes[0] is equal to 3.</param> /// <param name="result">The result of the index operation.</param> /// <returns>true if the operation is successful; otherwise, false. If this method returns /// false, the run-time binder of the language determines the behavior. (In most /// cases, a run-time exception is thrown.)</returns> public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { if (indexes != null && indexes.Length == 1) { ILuaValue o; lock (this) o = GlobalsTable.GetItemRaw(_runtime.CreateValue(indexes[0])); MethodInfo asMethod = typeof(ILuaValue).GetMethod("As").MakeGenericMethod(binder.ReturnType); result = asMethod.Invoke(o, null); if (result is double) result = new NumberProxy((double)result); return true; } else return base.TryGetIndex(binder, indexes, out result); }