public void TestAccessProperty_Type() { IReflector r = Reflector.Bind(typeof(ReflectSampleClass1), ReflectorPolicy.TypeAllIgnoreCase); Assert.IsTrue(r.ExistProperty("P")); Assert.IsTrue(r.ExistProperty("p")); }
private static void CopyBySource(IDictionary <string, string> src, object dest, bool throwOnNotExist) { IReflector r = Reflector.Bind(dest); foreach (var key in src.Keys) { if (!r.ExistProperty(key)) { if (throwOnNotExist) { ExceptionHelper.ThrowDestPropertyNotExist(dest.GetType(), key); } else { _log.Warning("Destination type [{0}] does not have property [{1}].", dest.GetType(), key); } } else { try { Type pType = r.GetPropertyType(key); object val = TypeCast.ChangeToTypeOrNullableType(src[key], pType); r.SetPropertyValue(key, val); } catch (Exception ex) { throw ex.CreateWrapException <CopyException>(); } } } }
public void TestAccessProperty_InstancePrivate() { ReflectSampleClass1 o = new ReflectSampleClass1(); IReflector r = Reflector.Bind(o, ReflectorPolicy.InstanceAllIgnoreCase); Type t = r.GetPropertyType("Name"); Assert.IsTrue(t == typeof(string)); object val = r.GetPropertyValue("Name"); Assert.AreEqual("sample1", val); Assert.IsTrue(r.ExistProperty("name")); Assert.IsTrue(r.ExistProperty("Name")); Assert.IsFalse(r.ExistProperty("P")); }
public void TestAccessProperty_InstancePublic() { ReflectSampleClass1 o = new ReflectSampleClass1(); IReflector r = Reflector.Bind(o, ReflectorPolicy.InstancePublicIgnoreCase); Type t = r.GetPropertyType("count"); Assert.IsTrue(t == typeof(int)); object val = r.GetPropertyValue("Count"); Assert.AreEqual(3, val); Assert.IsTrue(r.ExistProperty("count")); Assert.IsTrue(r.ExistProperty("Count")); r.SetPropertyValue("count", 10); Assert.AreEqual(10, o.Count); }
public override object Visit(IndexerNode obj) { object instance = obj.Variable.Accept(this); object[] paramArray = (object[])obj.Args.Accept(this); if (instance == null) { ExceptionHelper.ThrowEvalNull(); } ISmartInvoker invoker; ClassDefination cdef = instance as ClassDefination; if (cdef != null) { invoker = EvalSmartInvoker.CreateInstance(cdef.ObjType, false); } else { invoker = EvalSmartInvoker.CreateInstance(instance, true); } if (instance.GetType().IsArray)//Note:invoke "Get" on array { return(invoker.Invoke("Get", paramArray)); } else//Note:invoke indexer on instance { try//Note:Try indexer first { return(invoker.Invoke("get_Item", paramArray)); } catch//Note:then try property { if (paramArray.Length == 1 && paramArray[0] != null) { string propertyName = paramArray[0].ToString(); IReflector r = Reflector.Bind(instance); if (r.ExistProperty(propertyName)) { return(r.GetPropertyValue(propertyName)); } else if (r.ExistField(propertyName)) { return(r.GetFieldValue(propertyName)); } else { ExceptionHelper.ThrowIndexOrPropertyNotExist(instance.GetType(), propertyName); } } //Note:Can not convert to property, throw throw; } } }
private static void CopyByBoth(object src, object dest) { IReflector rSrc = Reflector.Bind(src); IReflector rDest = Reflector.Bind(dest); foreach (var pName in rSrc.FindAllPropertyNames()) { if (rDest.ExistProperty(pName)) { rDest.SetPropertyValue(pName, rSrc.GetPropertyValue(pName)); } } }
private static void CopyByDest(object src, object dest, bool throwOnNotExist) { IReflector rSrc = Reflector.Bind(src); IReflector rDest = Reflector.Bind(dest); foreach (var pName in rDest.FindAllPropertyNames()) { if (!rSrc.ExistProperty(pName)) { if (throwOnNotExist) { ExceptionHelper.ThrowSourceKeyOrPropertyNotExist(pName); } } rDest.SetPropertyValue(pName, rSrc.GetPropertyValue(pName)); } }
private static void CopyByBoth(IDictionary <string, string> src, object dest) { IReflector r = Reflector.Bind(dest); foreach (var key in src.Keys) { if (r.ExistProperty(key)) { try { Type pType = r.GetPropertyType(key); object val = TypeCast.ChangeToTypeOrNullableType(src[key], pType); r.SetPropertyValue(key, val); } catch (Exception ex) { throw ex.CreateWrapException <CopyException>(); } } } }