/// <summary> /// Called whenever property in this virtual object changes /// </summary> /// <param name="aObject"> /// Object related to change <see cref="IVirtualObject"/> /// </param> /// <param name="aProperty"> /// Property that changed <see cref="VirtualProperty"/> /// </param> public virtual void Changed(IVirtualObject aObject, VirtualProperty aProperty) { if (onChange != null) { onChange(aObject, aProperty); } }
/// <summary> /// Attempt to perform a dynamic cast on an object. This will throw an exception if source or target type is unsupported /// or if the RTTI cast function has not been set up! /// </summary> /// <param name="obj">The object.</param> /// <param name="target">Type of object to cast to.</param> /// <returns></returns> internal static IVirtualObject DynamicCast(IVirtualObject obj, Type target) { if (obj == null || obj.Address == IntPtr.Zero) { return(null); } if (target == typeof(IVirtualObject)) { return(obj); } // Get target RTTI. There may be more than one if we combined types. Tuple <Type, uint[], IntPtr> targetInfo; { if (!MapInterface.TryGetValue(target, out targetInfo)) { throw new NotSupportedException("Missing RTTI type descriptor for target \"" + target.Name + "\"!"); } } // Try multiple RTTI addresses. foreach (var ip in targetInfo.Item2) { var result = Custom_RTTI_Cast(obj.Address, ip, targetInfo.Item3); if (result != IntPtr.Zero) { return((IVirtualObject)MemoryObject.FromAddress(target, result)); } } // None matched or didn't have any in the list. return(null); }
/// <summary> /// Creates VirtualProperty /// </summary> /// <param name="a_Master"> /// Master object <see cref="IVirtualObject"/> /// </param> /// <param name="a_Name"> /// Property name <see cref="System.String"/> /// </param> /// <param name="a_PropertyType"> /// Type of value <see cref="System.Type"/> /// </param> /// <param name="a_Value"> /// Property value <see cref="System.Object"/> /// </param> public VirtualProperty(IVirtualObject a_Master, string a_Name, System.Type a_PropertyType, object a_Value) { master.Target = a_Master; onChange += a_Master.Changed; name = a_Name; propertyType = a_PropertyType; propertyValue = a_Value; onChange = null; }
/// <summary> /// Creates VirtualObject /// </summary> /// <param name="a_Name"> /// Name of object <see cref="System.String"/> /// </param> /// <param name="a_Object"> /// Template object <see cref="IVirtualObject"/> /// </param> internal VirtualObject(string a_Name, IVirtualObject a_Object) { ObjectTypeName = a_Name; if (a_Object != null) { InheritStrict(a_Object); CopyFrom(a_Object); } }
/// <summary> /// Creates VirtualObject /// </summary> /// <param name="a_Object"> /// Template object <see cref="IVirtualObject"/> /// </param> public VirtualObject(IVirtualObject a_Object) { if (a_Object != null) { ObjectTypeName = a_Object.ObjectType.Name; InheritStrict(a_Object); CopyFrom(a_Object); } }
/// <summary> /// Copies all data to object /// </summary> /// <param name="a_From"> /// Source object <see cref="IVirtualObject"/> /// </param> /// <param name="a_To"> /// Destination object <see cref="IVirtualObject"/> /// </param> public static void CopyDataTo(IVirtualObject a_From, IVirtualObject a_To) { if ((a_From == null) && (a_To == null)) { return; } for (int i = 0; i < a_From.ObjectType.Count; i++) { VirtualProperty vp = a_To[a_From[i].Name]; if (vp != null) { if (TypeValidator.IsCompatible(vp.PropertyType, a_From[i].PropertyType) == true) { vp.Value = a_From[i].Value; } } vp = null; } }
/// <summary> /// Copies all data to object /// </summary> /// <param name="a_From"> /// Source object <see cref="System.Object"/> /// </param> /// <param name="a_To"> /// Destination object <see cref="IVirtualObject"/> /// </param> public static void CopyDataFrom(object a_From, IVirtualObject a_To) { if ((a_From == null) && (a_To == null)) { return; } foreach (PropertyInfo prop in a_From.GetType().GetProperties()) { VirtualProperty vp = a_To[prop.Name]; if ((prop != null) && (vp != null)) { if (prop.CanRead == true) { vp.Value = prop.GetValue(a_From, null); } } vp = null; } }
/// <summary> /// Copies all data to object /// </summary> /// <param name="a_From"> /// Source object <see cref="IVirtualObject"/> /// </param> /// <param name="a_To"> /// Destination object <see cref="System.Object"/> /// </param> public static void CopyDataTo(IVirtualObject a_From, object a_To) { if ((a_From == null) && (a_To == null)) { return; } foreach (PropertyInfo prop in a_To.GetType().GetProperties()) { VirtualProperty vp = a_From[prop.Name]; if ((prop != null) && (vp != null)) { if (prop.CanWrite == true) { if (TypeValidator.IsCompatible(vp.PropertyType, prop.PropertyType) == true) { prop.SetValue(a_To, vp.Value, null); } } } vp = null; } }
public static void OnPropChange(IVirtualObject a_Object, VirtualProperty a_Property) { Console.WriteLine("Changed value in (" + a_Object + "::" + a_Object.ObjectType.Name + ") on " + a_Property.Name); }
/// <summary> /// Creates unique VirtualObject /// </summary> /// <param name="a_Name"> /// Name of new object <see cref="System.String"/> /// </param> /// <param name="a_Object"> /// Ineriting type and value from <see cref="IVirtualObject"/> /// </param> public UniqueVirtualObject(string a_Name, IVirtualObject a_Object) : base(a_Name, a_Object) { }
/// <summary> /// Copies all data from object /// </summary> /// <param name="a_Object"> /// Source object <see cref="IVirtualObject"/> /// </param> public void CopyFrom(IVirtualObject a_Object) { VirtualObject.CopyDataTo(a_Object, this); }
/// <summary> /// Copies all data to object /// </summary> /// <param name="a_Object"> /// Destination object <see cref="IVirtualObject"/> /// </param> public void CopyTo(IVirtualObject a_Object) { VirtualObject.CopyDataTo(this, a_Object); }
/// <summary> /// Inherits the properties from other type /// </summary> /// <param name="a_Object"> /// Object to inherit from <see cref="IVirtualObject"/> /// </param> /// <returns> /// true <see cref="System.Boolean"/> /// </returns> /// <remarks> /// If property already exists, it avoids copying it /// /// also adds properties /// </remarks> public bool InheritRelaxed(IVirtualObject a_Object) { return(InheritRelaxed(a_Object.ObjectType)); }
/// <summary> /// Inherits the properties from other type /// </summary> /// <param name="a_Object"> /// Object to inherit from <see cref="IVirtualObject"/> /// </param> /// <returns> /// true <see cref="System.Boolean"/> /// </returns> /// <remarks> /// Throws Exception if property already exists /// /// also adds properties /// </remarks> public bool InheritStrict(IVirtualObject a_Object) { return(InheritStrict(a_Object.ObjectType)); }
public static void OnPropChange (IVirtualObject a_Object, VirtualProperty a_Property) { Console.WriteLine ("Changed value in (" + a_Object + "::" + a_Object.ObjectType.Name + ") on " + a_Property.Name); }