public void GenericCallUtilityTestsSimplePasses() { var s = new Sample(); { var mi = s.GetType().GetMethod("Set"); var action = GenericInvokeCallFactory.OpenAction <Sample, int>(mi); action(s, 1); Assert.AreEqual(1, s.Value); } { var mi = s.GetType().GetMethod("Get"); var func = GenericInvokeCallFactory.OpenFunc <Sample, int, int>(mi); /*var value =*/ func(s, 1); Assert.AreEqual(1, s.Value); } { var mi = s.GetType().GetMethod("Set"); var action = GenericExpressionCallFactory.Create <Sample, int>(mi); action(s, 2); Assert.AreEqual(2, s.Value); } }
private static Action <IFormatter, T> GetSerializer() { var t = typeof(T); // object if (typeof(T) == typeof(object) && t.GetType() != typeof(object)) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeObject"); return(GenericInvokeCallFactory.StaticAction <IFormatter, T>(mi)); } try { // primitive var mi = typeof(IFormatter).GetMethod("Value", new Type[] { t }); if (mi != null) { return(GenericInvokeCallFactory.OpenAction <IFormatter, T>(mi)); } } catch (AmbiguousMatchException) { // do nothing } { // dictionary var idictionary = t.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDictionary <,>) && x.GetGenericArguments()[0] == typeof(string) ); if (idictionary != null) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeDictionary"); return(GenericInvokeCallFactory.StaticAction <IFormatter, T>(mi)); } } { // object[] if (t == typeof(object[])) { var mi = FormatterExtensionsSerializer.GetMethod("SerializeObjectArray"); return(GenericInvokeCallFactory.StaticAction <IFormatter, T>(mi)); } } { // list var ienumerable = t.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable <>) ); if (ienumerable != null) { var g = FormatterExtensionsSerializer.GetMethod("SerializeArray"); var mi = g.MakeGenericMethod(ienumerable.GetGenericArguments()); return(GenericInvokeCallFactory.StaticAction <IFormatter, T>(mi)); } } { // reflection var schema = JsonSchema.FromType <T>(); return((IFormatter f, T value) => { var c = new JsonSchemaValidationContext(value) { EnableDiagnosisForNotRequiredFields = true }; schema.Serialize(f, value, c); }); } //throw new NotImplementedException(); }