Ejemplo n.º 1
0
        public void TestTypeConverter()
        {
            object tNew         = Build.NewObject(ColorViaString: "Blue");
            var    tColorHolder = tNew.ActLike <IColor>();

            Assert.AreEqual(Colors.Blue, tColorHolder.ColorViaString);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        private static IPaged GetTypedPagedObject(object input)
        {
            if (input == null)
            {
                return(null);
            }

            IPagedList pagedList;

            return(IsPagedListCompatible(input, out pagedList)
                ? new PagedListShim(pagedList)
                : input.ActLike <IPaged>());
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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;
            }
        }
Ejemplo n.º 8
0
        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));
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
 public TInterface DuckType <TInterface>(object originalDynamic) where TInterface : class
 {
     return(originalDynamic.ActLike <TInterface>());
 }
Ejemplo n.º 11
0
 /// <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) }));
 }
Ejemplo n.º 12
0
 /// <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>());
 }
Ejemplo n.º 13
0
 protected TInterface Stub <TInterface>(object prototype)
     where TInterface : class
 {
     return(prototype.ActLike <TInterface>());
 }