public static IProxyPropertyInfo AsProxy(this PropertyInfo info) { IProxyType proxyType = factory.Get(info.DeclaringType); if (info.GetGetMethod().IsPublic) return proxyType.GetProperty(info.Name); return proxyType.GetProperty(info.Name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); }
public IProxyPropertyInfo GetProperty(string name, BindingFlags flags) { IProxyPropertyInfo info; if (this.properties.TryGetValue(name, out info)) { return(info); } PropertyInfo propertyInfo = this.type.GetProperty(name, flags); if (propertyInfo != null && propertyInfo.DeclaringType.Equals(type)) { return(this.CreateProxyPropertyInfo(propertyInfo)); } IProxyType baseTypeInfo = this.GetBase(); if (baseTypeInfo == null) { return(null); } return(baseTypeInfo.GetProperty(name, flags)); }
public static void Run() { Account account = new Account() { ID = 1, Username = "******", Password = "******", Email = "*****@*****.**", Birthday = new DateTime(2000, 3, 3) }; User user = new User() { FirstName = "Tom" }; try { IProxyType userProxyType = ProxyFactory.Default.Get(typeof(User)); UnityEngine.Debug.LogFormat("user.FirstName:{0}", userProxyType.GetProperty("FirstName").GetValue(user)); } catch (Exception e) { UnityEngine.Debug.LogFormat("user exception:{0}", e); } try { IProxyType accountProxyType = ProxyFactory.Default.Get(typeof(Account)); UnityEngine.Debug.LogFormat("account.Username:{0}", accountProxyType.GetProperty("Username").GetValue(account)); } catch (Exception e) { UnityEngine.Debug.LogFormat("account exception:{0}", e); } //Test t = new Test() //{ // Username = "******" //}; //try //{ // IProxyType testProxyType = ProxyFactory.Default.Get(typeof(Test)); // UnityEngine.Debug.LogFormat("test.Username:{0}", testProxyType.GetProperty("Username").GetValue(t)); //} //catch (Exception e) //{ // UnityEngine.Debug.LogFormat("user exception:{0}", e); //} }
public ITargetProxy CreateProxy(object target, BindingDescription description) { IProxyType type = target.GetType().AsProxy(); IProxyMemberInfo memberInfo = type.GetMember(description.TargetName); if (memberInfo == null) { memberInfo = type.GetMember(description.TargetName, BindingFlags.Instance | BindingFlags.NonPublic); } if (memberInfo == null) { throw new MissingMemberException(type.Type.FullName, description.TargetName); } EventListener updateTrigger = null; if (!string.IsNullOrEmpty(description.UpdateTrigger)) { IProxyPropertyInfo updateTriggerPropertyInfo = type.GetProperty(description.UpdateTrigger); IProxyFieldInfo updateTriggerFieldInfo = updateTriggerPropertyInfo == null?type.GetField(description.UpdateTrigger) : null; if (updateTriggerPropertyInfo != null) { updateTrigger = updateTriggerPropertyInfo.GetValue(target) as EventListener; } if (updateTriggerFieldInfo != null) { updateTrigger = updateTriggerFieldInfo.GetValue(target) as EventListener; } if (updateTriggerPropertyInfo == null && updateTriggerFieldInfo == null) { throw new MissingMemberException(type.Type.FullName, description.UpdateTrigger); } //Other Property Type if (updateTrigger == null) /* by UniversalTargetProxyFactory */ { return(null); } } var propertyInfo = memberInfo as IProxyPropertyInfo; if (propertyInfo != null) { if (typeof(IObservableProperty).IsAssignableFrom(propertyInfo.ValueType)) { return(null); } if (typeof(EventListener).IsAssignableFrom(propertyInfo.ValueType)) { //Event Type object listener = propertyInfo.GetValue(target); if (listener == null) { throw new NullReferenceException(propertyInfo.Name); } return(new FairyEventProxy(target, (EventListener)listener)); } //Other Property Type if (updateTrigger == null)/* by UniversalTargetProxyFactory */ { return(null); } return(new FairyPropertyProxy(target, propertyInfo, updateTrigger)); } var fieldInfo = memberInfo as IProxyFieldInfo; if (fieldInfo != null) { if (typeof(IObservableProperty).IsAssignableFrom(fieldInfo.ValueType)) { return(null); } if (typeof(EventListener).IsAssignableFrom(fieldInfo.ValueType)) { //Event Type object listener = fieldInfo.GetValue(target); if (listener == null) { throw new NullReferenceException(fieldInfo.Name); } return(new FairyEventProxy(target, (EventListener)listener)); } //Other Property Type if (updateTrigger == null)/* by UniversalTargetProxyFactory */ { return(null); } return(new FairyFieldProxy(target, fieldInfo, updateTrigger)); } return(null); }
public static IProxyPropertyInfo AsProxy(this PropertyInfo info) { IProxyType proxyType = factory.Create(info.DeclaringType); return(proxyType.GetProperty(info)); }
//private static readonly ILog log = LogManager.GetLogger(typeof(UnityTargetProxyFactory)); public ITargetProxy CreateProxy(object target, BindingDescription description) { IProxyType type = target.GetType().AsProxy(); IProxyMemberInfo memberInfo = type.GetMember(description.TargetName); if (memberInfo == null) { memberInfo = type.GetMember(description.TargetName, BindingFlags.Instance | BindingFlags.NonPublic); } if (memberInfo == null) { return(null); } UnityEventBase updateTrigger = null; if (!string.IsNullOrEmpty(description.UpdateTrigger)) { var updateTriggerPropertyInfo = type.GetProperty(description.UpdateTrigger); if (updateTriggerPropertyInfo != null) { updateTrigger = updateTriggerPropertyInfo.GetValue(target) as UnityEventBase; } if (updateTriggerPropertyInfo == null) { var updateTriggerFieldInfo = type.GetField(description.UpdateTrigger); if (updateTriggerFieldInfo != null) { updateTrigger = updateTriggerFieldInfo.GetValue(target) as UnityEventBase; } } } var propertyInfo = memberInfo as IProxyPropertyInfo; if (propertyInfo != null) { if (typeof(IObservableProperty).IsAssignableFrom(propertyInfo.ValueType)) { return(null); } if (typeof(UnityEventBase).IsAssignableFrom(propertyInfo.ValueType)) { //Event Type object unityEvent = propertyInfo.GetValue(target); Type[] paramTypes = GetUnityEventParametersType(propertyInfo.ValueType); return(CreateUnityEventProxy(target, (UnityEventBase)unityEvent, paramTypes)); } //Other Property Type if (updateTrigger == null)/* by UniversalTargetProxyFactory */ { return(null); } return(CreateUnityPropertyProxy(target, propertyInfo, updateTrigger)); } var fieldInfo = memberInfo as IProxyFieldInfo; if (fieldInfo != null) { if (typeof(IObservableProperty).IsAssignableFrom(fieldInfo.ValueType)) { return(null); } if (typeof(UnityEventBase).IsAssignableFrom(fieldInfo.ValueType)) { //Event Type object unityEvent = fieldInfo.GetValue(target); Type[] paramTypes = GetUnityEventParametersType(fieldInfo.ValueType); return(CreateUnityEventProxy(target, (UnityEventBase)unityEvent, paramTypes)); } //Other Property Type if (updateTrigger == null)/* by UniversalTargetProxyFactory */ { return(null); } return(CreateUnityFieldProxy(target, fieldInfo, updateTrigger)); } return(null); }