public void TestDelegateFactory() { TestObject obj = new TestObject(); IFactory <TestObject> factory = new DelegateFactory <TestObject>(() => obj); Assert.IsTrue(ReferenceEquals(factory.Create(), factory.Create())); factory = new DelegateFactory <TestObject>(() => new TestObject()); Assert.IsFalse(ReferenceEquals(factory.Create(), factory.Create())); }
public void TestEvents(string eventName) { var ev = instance.GetType().GetEvent(eventName); var del = DelegateFactory.Create(ev, Handler); ev.AddEventHandler(instance, del); }
void Write(StringBuilder builder, Type t, object obj) { if (obj == null) { return; } IEnumerable <PropertyInfo> properties; if (!typeToProperties.TryGetValue(t, out properties)) { throw new InvalidOperationException(string.Format("Type {0} was not registered in the serializer. Check that it appears in the list of configured assemblies/types to scan.", t.FullName)); } foreach (var prop in properties) { if (IsIndexedProperty(prop)) { throw new NotSupportedException(string.Format("Type {0} contains an indexed property named {1}. Indexed properties are not supported on message types.", t.FullName, prop.Name)); } WriteEntry(prop.Name, prop.PropertyType, DelegateFactory.Create(prop).Invoke(obj), builder); } foreach (var field in typeToFields[t]) { WriteEntry(field.Name, field.FieldType, DelegateFactory.Create(field).Invoke(obj), builder); } }
public static object GetValue(this MemberInfo member, object source) { var fieldInfo = member as FieldInfo; if (fieldInfo != null) { var field = DelegateFactory.Create(fieldInfo); return(field.Invoke(source)); } var propertyInfo = (PropertyInfo)member; if (!propertyInfo.CanRead) { if (propertyInfo.PropertyType.IsValueType) { return(Activator.CreateInstance(propertyInfo.PropertyType)); } return(null); } var property = DelegateFactory.Create(propertyInfo); return(property.Invoke(source)); }
void ITester.Setup() { _mi1 = DelegateFactory.Create(typeof(PublicReadMethods).GetMethod("TestString")); _mi2 = DelegateFactory.Create(typeof(PublicReadMethods).GetMethod("TestInt")); _stringMethodParams = new object[] { "lalamido" }; _intMethodParams = new object[] { 1, 2 }; }
public static void Run() { var factory = new DelegateFactory(); // begin var d = factory.Create(); // end d.DynamicInvoke(42); }
public static void Run() { var factory = new DelegateFactory(); var action = new CustomAction<int>(delegate { }); // begin var d = factory.Create(action); // end d.DynamicInvoke(42); }
public static void Run() { var factory = new DelegateFactory(); // begin var d = factory.Create(); // end d.DynamicInvoke(42); }
private DelegateFactory.LateBoundMethod GetLateBoundMethod(MethodInfo methodInfo) { DelegateFactory.LateBoundMethod lateBoundMethod; if (!_lateBoundMethodCache.TryGetValue(methodInfo, out lateBoundMethod)) { lateBoundMethod = DelegateFactory.Create(methodInfo); _lateBoundMethodCache[methodInfo] = lateBoundMethod; } return(lateBoundMethod); }
public static void Run() { var action = new Action <int>(i => { }); var factory = new DelegateFactory(); // begin var d = factory.Create(action); // end d.DynamicInvoke(42); }
private Func <string, object> BuildFactoryMethod(Type grainType) { var mi = _grainIdentityInterfaceMap.FirstOrDefault(x => x.Item1.IsAssignableFrom(grainType)); if (mi != null) { var factoryDelegate = DelegateFactory.Create(mi.Item2.GetGenericMethodDefinition().MakeGenericMethod(grainType)); //Construct the GrainKey parameter var idParts = GetArgumentParser(mi.Item2.GetParameters()); return((id) => factoryDelegate(_grainProxy, idParts(id))); } throw new OrleansGrainReferenceException($"cannot construct grain {grainType.Name}"); }
public void Delegate_factory_create_method_timing() { var stopWatch = new Stopwatch(); stopWatch.Start(); var method = typeof(string).GetMethod("ToUpper", new Type[] { }); DelegateFactory.Create(method); stopWatch.Stop(); Console.WriteLine("Totally took: {0}ms", stopWatch.ElapsedMilliseconds); }
public void String_test_with_delegate() { var stopWatch = new Stopwatch(); stopWatch.Start(); var methodInfo = typeof(string).GetMethod("ToUpper", new Type[] {}); var delMethod = DelegateFactory.Create(methodInfo); for (var i = 0; i < Times; i++) { var result = delMethod(TextValue, new object[] { }); } stopWatch.Stop(); Console.WriteLine("Delegate took: {0}ms", stopWatch.ElapsedMilliseconds); }
public override void HandleMessage(object instance, Message msg) { switch (msg) { case SubscribeToEventCommand sec: using (_locker.Lock()) { var eventInfo = instance.GetType().GetEvent(sec.EventName); var del = DelegateFactory.Create(eventInfo, EventHandler); _events[sec.EventName].Add(del); eventInfo.AddEventHandler(instance, del); } break; case UnsubscribeFromEventCommand uec: using (_locker.Lock()) { var eventInfo = instance.GetType().GetEvent(uec.EventName); var delegates = _events[uec.EventName]; if (delegates.Count > 0) { var del = delegates[0]; delegates.Remove(del); eventInfo.RemoveEventHandler(instance, del); } } break; case EventInvokeCommand eic: List <Delegate> invokeList; using (_locker.Lock()) invokeList = _events[eic.EventName].ToList(); invokeList.ForEach(it => it.DynamicInvoke(eic.Arguments)); break; default: Successor?.HandleMessage(instance, msg); break; } }
public void String_test_with_delegate() { var stopWatch = new Stopwatch(); stopWatch.Start(); #if NETCORE var methodInfo = typeof(string).GetTypeInfo().GetDeclaredMethods("ToUpper").First(m => m.GetParameters().Length == 0); #else var methodInfo = typeof(string).GetMethod("ToUpper", new Type[] { }); #endif var delMethod = DelegateFactory.Create(methodInfo); for (var i = 0; i < Times; i++) { delMethod(TextValue, new object[] { }); } stopWatch.Stop(); Console.WriteLine("Delegate took: {0}ms", stopWatch.ElapsedMilliseconds); }
/// <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); } }
/// <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); } }
public void TestDelegateFactory() { TestObject obj = new TestObject(); IFactory<TestObject> factory = new DelegateFactory<TestObject>(() => obj); Assert.IsTrue(ReferenceEquals(factory.Create(), factory.Create())); factory = new DelegateFactory<TestObject>(() => new TestObject()); Assert.IsFalse(ReferenceEquals(factory.Create(), factory.Create())); }