public DataTableToMetaDataMapper() { var allProperties = typeof(T).AllProperties().Where(propertyCanBeMapped); _propertyCache = new Cache <PropertyInfo, SetHandler>(); allProperties.Each(x => _propertyCache.Add(x, DelegateFactory.CreateSet(x))); }
public FieldAccessor(FieldInfo fieldInfo) : base(fieldInfo.Name, fieldInfo.FieldType) { _fieldInfo = fieldInfo; _getHandler = DelegateFactory.CreateGet(fieldInfo); _setHandler = DelegateFactory.CreateSet(fieldInfo); _isInternal = _fieldInfo.IsPrivate || _fieldInfo.IsFamily; }
public PropertyAccessor(PropertyInfo propertyInfo) : base(propertyInfo) { _hasSetter = propertyInfo.GetSetMethod(true) != null; if (_hasSetter) { _lateBoundPropertySet = DelegateFactory.CreateSet(propertyInfo); } }
public void Should_set_property_when_property_is_a_value_type_and_type_is_interface() { var sourceType = typeof(ISource); PropertyInfo property = sourceType.GetProperty("Value"); LateBoundPropertySet callback = DelegateFactory.CreateSet(property); var source = new Source(); callback(source, 5); source.Value.ShouldEqual(5); }
public void Should_set_field_when_field_is_a_reference_type() { var sourceType = typeof(Source); FieldInfo field = sourceType.GetField("Value3"); LateBoundFieldSet callback = DelegateFactory.CreateSet(field); var source = new Source(); callback(source, "hello"); source.Value3.ShouldEqual("hello"); }
public void Should_set_property_when_property_is_a_reference_type() { var sourceType = typeof(Source); PropertyInfo property = sourceType.GetProperty("Value4"); LateBoundPropertySet callback = DelegateFactory.CreateSet(property); var source = new Source(); callback(source, "hello"); source.Value4.ShouldEqual("hello"); }
/// <summary> /// Initializes a new instance of the <see cref="FieldAccessor"/> class. /// </summary> /// <param name="fieldInfo">The <see cref="FieldInfo"/> instance to use for this accessor.</param> public FieldAccessor(FieldInfo fieldInfo) { _fieldInfo = fieldInfo; _name = fieldInfo.Name; _memberType = fieldInfo.FieldType; _hasGetter = true; _lateBoundGet = new Lazy <LateBoundGet>(() => DelegateFactory.CreateGet(_fieldInfo)); _hasSetter = !fieldInfo.IsInitOnly && !fieldInfo.IsLiteral; if (_hasSetter) { _lateBoundSet = new Lazy <LateBoundSet>(() => DelegateFactory.CreateSet(_fieldInfo)); } }
object GetObjectOfTypeFromNode(Type t, XmlNode node) { if (t.IsSimpleType() || t == typeof(Uri)) { return(GetPropertyValue(t, node)); } if (typeof(IEnumerable).IsAssignableFrom(t)) { return(GetPropertyValue(t, node)); } var result = mapper.CreateInstance(t); foreach (XmlNode n in node.ChildNodes) { Type type = null; if (n.Name.Contains(":")) { type = Type.GetType("System." + n.Name.Substring(0, n.Name.IndexOf(":")), false, true); } var prop = GetProperty(t, n.Name); if (prop != null) { var val = GetPropertyValue(type ?? prop.PropertyType, n); if (val != null) { var propertySet = DelegateFactory.CreateSet(prop); propertySet.Invoke(result, val); continue; } } var field = GetField(t, n.Name); if (field != null) { var val = GetPropertyValue(type ?? field.FieldType, n); if (val != null) { var fieldSet = DelegateFactory.CreateSet(field); fieldSet.Invoke(result, val); } } } return(result); }
public static void SetValue(this MemberInfo member, object target, object value) { var fieldInfo = member as FieldInfo; if (fieldInfo != null) { var fieldSet = DelegateFactory.CreateSet(fieldInfo); fieldSet.Invoke(target, value); } else { var propertyInfo = member as PropertyInfo; var propertySet = DelegateFactory.CreateSet(propertyInfo); propertySet.Invoke(target, value); } }
public void CreateSetField() { TestClass.fieldStaticKey = "field test key"; var testClass = new TestClass(); var fieldInfos = typeof(TestClass).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); Stopwatch watch = Stopwatch.StartNew(); foreach (var fieldInfo in fieldInfos) { object value = null; switch (fieldInfo.Name) { case "fieldAge": value = 21; break; case "fieldIsStatic": value = true; break; case "fieldStaticKey": value = "Static"; break; case "fieldName": value = "John Doe"; break; default: continue; } var d = DelegateFactory.CreateSet(fieldInfo); Assert.IsNotNull(d); d(testClass, value); Console.WriteLine("Field: {0} Value: {1}", fieldInfo.Name, value); } watch.Stop(); Console.WriteLine("Time: {0}ms", watch.ElapsedMilliseconds); Assert.IsNotNull(testClass); }
/// <summary> /// Initializes a new instance of the <see cref="PropertyAccessor"/> class. /// </summary> /// <param name="propertyInfo">The <see cref="PropertyInfo"/> instance to use for this accessor.</param> public PropertyAccessor(PropertyInfo propertyInfo) { _propertyInfo = propertyInfo; _name = _propertyInfo.Name; _memberType = _propertyInfo.PropertyType; _hasGetter = _propertyInfo.GetGetMethod(true) != null; if (_hasGetter) { _lateBoundGet = new Lazy <LateBoundGet>(() => DelegateFactory.CreateGet(_propertyInfo)); } _hasSetter = propertyInfo.GetSetMethod(true) != null; if (_hasSetter) { _lateBoundSet = new Lazy <LateBoundSet>(() => DelegateFactory.CreateSet(_propertyInfo)); } }
public ReadWritePropertyAccessor(PropertyInfo propertyInfo) : base(propertyInfo) { _setHandler = DelegateFactory.CreateSet(propertyInfo); }
/// <summary> /// Scans the given type storing maps to fields and properties to save on reflection at runtime. /// </summary> /// <param name="t"></param> public void InitType(Type t) { if (t.IsSimpleType()) { return; } if (typeof(IEnumerable).IsAssignableFrom(t)) { if (t.IsArray) { typesToCreateForArrays[t] = typeof(List <>).MakeGenericType(t.GetElementType()); } foreach (Type g in t.GetGenericArguments()) { InitType(g); } //Handle dictionaries - initalize relevant KeyValuePair<T,K> types. foreach (Type interfaceType in t.GetInterfaces()) { Type[] arr = interfaceType.GetGenericArguments(); if (arr.Length == 1) { if (typeof(IEnumerable <>).MakeGenericType(arr[0]).IsAssignableFrom(t)) { InitType(arr[0]); } } } return; } var isKeyValuePair = false; var args = t.GetGenericArguments(); if (args.Length == 2) { isKeyValuePair = (typeof(KeyValuePair <,>).MakeGenericType(args) == t); } if (args.Length == 1 && args[0].IsValueType) { if (typeof(Nullable <>).MakeGenericType(args) == t) { InitType(args[0]); return; } } //already in the process of initializing this type (prevents infinite recursion). if (typesBeingInitialized.Contains(t)) { return; } typesBeingInitialized.Add(t); var props = GetAllPropertiesForType(t, isKeyValuePair); typeToProperties[t] = props; var fields = GetAllFieldsForType(t); typeToFields[t] = fields; foreach (var p in props) { propertyInfoToLateBoundProperty[p] = DelegateFactory.Create(p); if (!isKeyValuePair) { propertyInfoToLateBoundPropertySet[p] = DelegateFactory.CreateSet(p); } InitType(p.PropertyType); } foreach (var f in fields) { fieldInfoToLateBoundField[f] = DelegateFactory.Create(f); if (!isKeyValuePair) { fieldInfoToLateBoundFieldSet[f] = DelegateFactory.CreateSet(f); } InitType(f.FieldType); } }
protected override void Context() { base.Context(); _setHandler = DelegateFactory.CreateSet(ReflectionHelper.AllFieldsFor(typeof(SimpleObject)).First()); }
protected override void Context() { base.Context(); _setHandler = DelegateFactory.CreateSet(ReflectionHelper.PropertyFor <SimpleObject, string>(x => x.OneProperty)); }
public FieldAccessor(FieldInfo fieldInfo) : base(fieldInfo) { _lateBoundFieldSet = DelegateFactory.CreateSet(fieldInfo); }
public void CreateSetProperty() { var testClass = new TestClass(); var properties = typeof(TestClass).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); Stopwatch watch = Stopwatch.StartNew(); foreach (var propertyInfo in properties) { var d = DelegateFactory.CreateSet(propertyInfo); Assert.IsNotNull(d); object value = null; switch (propertyInfo.Name) { case "LastName": value = "Doe"; break; case "Age": value = 21; break; case "PrivateSet": value = "Private Set"; break; case "PrivateGet": value = "Private Get"; break; case "Internal": value = "test"; break; case "IsStatic": value = true; break; case "StaticName": value = "CreateSetProperty"; break; case "FirstName": value = "John"; break; } d(testClass, value); Console.WriteLine("Property: {0} Value: {1}", propertyInfo.Name, value); } watch.Stop(); Console.WriteLine("Time: {0}ms", watch.ElapsedMilliseconds); Assert.AreEqual("Doe", testClass.LastName); Assert.AreEqual(21, testClass.Age); Assert.AreEqual("Private Set", testClass.PrivateSet); Assert.AreEqual("test", testClass.Internal); Assert.AreEqual(true, TestClass.IsStatic); Assert.AreEqual("CreateSetProperty", TestClass.StaticName); Assert.AreEqual("John", testClass.FirstName); }
/// <summary> /// Scans the given type storing maps to fields and properties to save on reflection at runtime. /// </summary> /// <param name="t"></param> public void InitType(Type t) { logger.Debug("Initializing type: " + t.AssemblyQualifiedName); if (t.IsSimpleType()) { return; } if (typeof(IEnumerable).IsAssignableFrom(t)) { if (t.IsArray) { typesToCreateForArrays[t] = typeof(List <>).MakeGenericType(t.GetElementType()); } foreach (Type g in t.GetGenericArguments()) { InitType(g); } //Handle dictionaries - initalize relevant KeyValuePair<T,K> types. foreach (Type interfaceType in t.GetInterfaces()) { Type[] arr = interfaceType.GetGenericArguments(); if (arr.Length == 1) { if (typeof(IEnumerable <>).MakeGenericType(arr[0]).IsAssignableFrom(t)) { InitType(arr[0]); } } } if (t.IsGenericType && t.IsInterface) //handle IEnumerable<Something> { var g = t.GetGenericArguments(); var e = typeof(IEnumerable <>).MakeGenericType(g); if (e.IsAssignableFrom(t)) { typesToCreateForEnumerables[t] = typeof(List <>).MakeGenericType(g); } } #if !NET35 if (t.IsGenericType && t.GetGenericArguments().Length == 1) { Type setType = typeof(ISet <>).MakeGenericType(t.GetGenericArguments()); if (setType.IsAssignableFrom(t)) //handle ISet<Something> { var g = t.GetGenericArguments(); var e = typeof(IEnumerable <>).MakeGenericType(g); if (e.IsAssignableFrom(t)) { typesToCreateForEnumerables[t] = typeof(List <>).MakeGenericType(g); } } } #endif return; } var isKeyValuePair = false; var args = t.GetGenericArguments(); if (args.Length == 2) { isKeyValuePair = (typeof(KeyValuePair <,>).MakeGenericType(args) == t); } if (args.Length == 1 && args[0].IsValueType) { if (args[0].GetGenericArguments().Any() || typeof(Nullable <>).MakeGenericType(args) == t) { InitType(args[0]); if (!args[0].GetGenericArguments().Any()) { return; } } } //already in the process of initializing this type (prevents infinite recursion). if (typesBeingInitialized.Contains(t)) { return; } typesBeingInitialized.Add(t); var props = GetAllPropertiesForType(t, isKeyValuePair); typeToProperties[t] = props; var fields = GetAllFieldsForType(t); typeToFields[t] = fields; foreach (var p in props) { logger.Debug("Handling property: " + p.Name); propertyInfoToLateBoundProperty[p] = DelegateFactory.Create(p); if (!isKeyValuePair) { propertyInfoToLateBoundPropertySet[p] = DelegateFactory.CreateSet(p); } InitType(p.PropertyType); } foreach (var f in fields) { logger.Debug("Handling field: " + f.Name); fieldInfoToLateBoundField[f] = DelegateFactory.Create(f); if (!isKeyValuePair) { fieldInfoToLateBoundFieldSet[f] = DelegateFactory.CreateSet(f); } InitType(f.FieldType); } }