public void TestTypeConverter() { object tNew = Build.NewObject(ColorViaString: "Blue"); var tColorHolder = tNew.ActLike <IColor>(); Assert.AreEqual(Colors.Blue, tColorHolder.ColorViaString); }
private static void Visit(object rawNode, Action <IDfaNode> visitor) { var node = rawNode.ActLike <IDfaNode>(); if (node.Literals?.Values != null) { foreach (var dictValue in node.Literals.Values) { Visit(dictValue, visitor); } } // Break cycles if (node.Parameters != null && !ReferenceEquals(rawNode, node.Parameters)) { Visit(node.Parameters, visitor); } // Break cycles if (node.CatchAll != null && !ReferenceEquals(rawNode, node.CatchAll)) { Visit(node.CatchAll, visitor); } if (node.PolicyEdges?.Values != null) { foreach (var dictValue in node.PolicyEdges.Values) { Visit(dictValue, visitor); } } visitor(node); }
public void EventDynamicPropertyTest() { object tPoco = Build.NewObject(Prop2: 3, Event: null, OnEvent: new ThisAction <object, EventArgs>((@this, obj, args) => @this.Event(obj, args))); IEvent tActsLike = tPoco.ActLike <IEvent>(); var tSet = false; tActsLike.Event += (obj, args) => tSet = true; tActsLike.OnEvent(null, null); Assert.AreEqual(true, tSet); }
private static IPaged GetTypedPagedObject(object input) { if (input == null) { return(null); } IPagedList pagedList; return(IsPagedListCompatible(input, out pagedList) ? new PagedListShim(pagedList) : input.ActLike <IPaged>()); }
public static FluentBuilder <TInterface> Configure <TInterface>(object target, bool supressErrors = true) where TInterface : class { if (!typeof(TInterface).IsInterface) { throw new ArgumentException("TInterface"); } /* automatic duck typing if needed */ var typedTarget = target.GetType().Implements(typeof(TInterface)) ? (TInterface)target : target.ActLike <TInterface>(); return(new FluentBuilder <TInterface>(typedTarget, supressErrors)); }
private static bool IsPagedListCompatible(object paged, out IPagedList pagedList) { var target = paged.ActLike <IPagedList>(); if (target.Has(x => x.TotalItemCount) && target.Has(x => x.PageSize) && target.Has(x => x.PageNumber)) { pagedList = target; return(true); } pagedList = null; return(false); }
private static T TryWrapHook <T>(object instance) where T : class { // FIXME: Use TypeKitchen for much higher performance, once method calling is well-tested. try { var prototype = typeof(T).GetMethods(); var example = instance.GetType().GetMethods(); var match = prototype.Any(l => example.Any(r => AreMethodsDuckEquivalent(l, r))) ? instance.ActLike <T>() : null; return(match); } catch (Exception e) { Console.WriteLine(e); throw; } }
public static T VerifyAndDeserialize <T>(this string data, string publicKey) { object m = JsonConvert.DeserializeObject <ExpandoObject>(data); ISignature sig = m.ActLike <ISignature>(); if (sig == null) { throw new System.Security.SecurityException("Could not verify integrity of request. Not a secure response."); } var signature = sig.Signature; sig.Signature = null; var original = JsonConvert.SerializeObject(m); if (!Verify(original, publicKey, Convert.FromBase64String(signature))) { throw new System.Security.SecurityException("Object does not contain a valid signature."); } return(JsonConvert.DeserializeObject <T>(data)); }
private static T TryWrapHook <T>(object instance) where T : class { var prototype = typeof(T).GetMethods(); var example = instance.GetType().GetMethods(); var match = prototype.Any(l => example.Any(r => AreMethodsDuckEquivalent(l, r))) ? instance.ActLike <T>() : null; return(match); }
public TInterface DuckType <TInterface>(object originalDynamic) where TInterface : class { return(originalDynamic.ActLike <TInterface>()); }
/// <summary>Dynamically casts a given <paramref name="instance" /> to an interface of type <typeparamref name="T" />.</summary> /// <typeparam name="T">Type of the interface to proxy.</typeparam> /// <param name="instance">Entity to be casted.</param> /// <returns>Entity of type <typeparamref name="T" />.</returns> public static T ActLike <T>(this object instance) where T : IEntity { return((T)instance.ActLike(new[] { typeof(T) })); }
/// <summary> /// Set Dependency Resolver using duck typing, so it can be used like Context.DependencyResolver.GetService(...) /// </summary> public void SetDependencyResolver(object dependencyResolver) { SetDependencyResolver(dependencyResolver.ActLike <IDependencyResolver>()); }
protected TInterface Stub <TInterface>(object prototype) where TInterface : class { return(prototype.ActLike <TInterface>()); }