Example #1
0
        private static Lazy <T, TMetadata> CreateStronglyTypedLazyOfTM <T, TMetadata>(Func <object> funcOfObject, object metadata)
        {
            Requires.NotNull(funcOfObject, nameof(funcOfObject));
            Requires.NotNullAllowStructs(metadata, nameof(metadata));

            return(new Lazy <T, TMetadata>(funcOfObject.As <T>(), (TMetadata)metadata));
        }
Example #2
0
        public void RegisterFunc <TType>(Func <TType> func)
        {
            CheckAlreadyAdded <TType>();

            var resolver = new FuncResolver(func.As <Func <object> >());

            _resolvers.Add(typeof(TType), resolver);
        }
Example #3
0
        public void As()
        {
            Func <object> fo     = () => 5;
            Func <int>    fi     = fo.As <int>();
            int           result = fi();

            Assert.Equal(5, result);
        }
        public static jQueryObject AddValidationRule(this jQueryObject element, string eventClass,
            Func<jQueryObject, string> rule)
        {
            if (element.Length == 0)
                return element;

            if (rule == null)
                throw new Exception("rule is null!");

            element.AddClass("customValidate")
                .Bind("customValidate." + eventClass, rule.As<jQueryEventHandler>());

            return element;
        }
        public void TestAsFunction()
        {
            var d = new DynInst();
            var t = d.As <Two>();

            t();

            var p = new {
                Two = new Func <bool>(() => {
                    Console.WriteLine("In Func<bool> for Two!");
                    return(true);
                })
            };

            var q = p.As <Two>();

            q();

            var z = new {
            };


            // this creates a dummy function now.

            var tz = z.As <Two>();

            tz();



            Func <bool> fTwo = new Func <bool>(() => {
                Console.WriteLine("In fTwo");
                return(true);
            });

            fTwo.As <Two>()();

            Assert.Throws <Exception>(() => {
                Func <string> fThree = new Func <string>(() => {
                    Console.WriteLine("In fThree");
                    return("true");
                });

                fThree.As <Two>()();
            });
        }
Example #6
0
        public static jQueryObject AddValidationRule(this jQueryObject element, string eventClass,
                                                     Func <jQueryObject, string> rule)
        {
            if (element.Length == 0)
            {
                return(element);
            }

            if (rule == null)
            {
                throw new Exception("rule is null!");
            }

            element.AddClass("customValidate")
            .Bind("customValidate." + eventClass, rule.As <jQueryEventHandler>());

            return(element);
        }
Example #7
0
        public void CheckForAcceptableTypes()
        {
            using (CaptureConsole) {
                var x = new ActualImplementation().As <ClientInterface>();
                var y = x.ActuallyReturnsString();
                var z = x.ActuallyReturnsFileStream();

                Console.WriteLine("Y is {0}", y.GetType().Name);
                Console.WriteLine("Z is {0}", z.GetType().Name);

                // this function doesn't match anything in the implemention
                // so a stub method gets created (which returns null)
                // MemoryStream a = x.ActuallyRetunsMemoryStream();
                // Assert.Null(a);

                // the clientinterface is more restricted than the implementation
                // but that's ok.
                MemoryStream ms = new MemoryStream();
                Assert.True(x.TakesAStream(ms));

                // the clientinterface is less restrictive than the implementation
                // and that's not ok.
                Assert.False(x.TakesAFileStream(ms));

                var shouldWork = new {
                    TakesAStream = new Func <Stream, bool>(stream => { return(stream != null); })
                }.As <ClientInterface>();

                Assert.True(shouldWork.TakesAStream(ms));

                var shouldNotWork = new {
                    TakesAFileStream = new Func <MemoryStream, bool>(stream => {
                        Console.WriteLine("never called");
                        return(stream != null);
                    })
                }.As <ClientInterface>();

                Assert.False(shouldWork.TakesAFileStream(ms));

                var shouldWorkToo = new {
                    ActuallyReturnsString = new Func <object>(() => "hello")
                }.As <ClientInterface>();

                Assert.NotNull(shouldWorkToo.ActuallyReturnsString());

                var shouldNotWorkToo = new {
                    ActuallyRetunsMemoryStream = new Func <Stream>(() => new MemoryStream())
                }.As <ClientInterface>();

                Assert.Null(shouldNotWorkToo.ActuallyRetunsMemoryStream());

                Func <object> fReturnsAString = new Func <object>(() => "hello");

                var fShouldWork = fReturnsAString.As <ReturnsAnObject>();

                Assert.NotNull(fShouldWork());

                Assert.Throws <Exception>(() => {
                    // this shouldn't work because the return type object
                    // can't be expressed as a string.
                    var fShouldNotWork = fReturnsAString.As <ReturnsAString>();
                });
            }
        }
 public static Predicate <T> AsPredicate <T>([NotNull] this Func <T, bool> function)
 {
     return(function.As <Predicate <T> >());
 }
 public static Comparison <T> AsComparison <T>([NotNull] this Func <T, T, int> function)
 {
     return(function.As <Comparison <T> >());
 }
Example #10
0
        private static Lazy <T> CreateStronglyTypedLazyOfT <T>(Func <object> funcOfObject, object metadata)
        {
            Requires.NotNull(funcOfObject, nameof(funcOfObject));

            return(new Lazy <T>(funcOfObject.As <T>()));
        }
 public SignalHandler(Func <ISubscriber, TInput, object?, CancellationToken, Task> handler, out string inputMessageName)
     : this(handler.As <MulticastDelegate>(), out inputMessageName)
 {
 }
Example #12
0
        public void TestAsFunction()
        {
            using (CaptureConsole) {
                var d = new DynInst();
                var t = d.As<Two>();
                t();

                var p = new {
                    Two = new Func<bool>(() => {
                        Console.WriteLine("In Func<bool> for Two!");
                        return true;
                    })
                };

                var q = p.As<Two>();
                q();

                var z = new {
                };

                // this creates a dummy function now.

                var tz = z.As<Two>();
                tz();

                Func<bool> fTwo = new Func<bool>(() => {
                    Console.WriteLine("In fTwo");
                    return true;
                });

                fTwo.As<Two>()();

                Assert.Throws<Exception>(() => {
                    Func<string> fThree = new Func<string>(() => {
                        Console.WriteLine("In fThree");
                        return "true";
                    });

                    fThree.As<Two>()();
                });
            }
        }
Example #13
0
        public void CheckForAcceptableTypes()
        {
            using (CaptureConsole) {
                var x = new ActualImplementation().As<ClientInterface>();
                var y = x.ActuallyReturnsString();
                var z = x.ActuallyReturnsFileStream();

                Console.WriteLine("Y is {0}", y.GetType().Name);
                Console.WriteLine("Z is {0}", z.GetType().Name);

                // this function doesn't match anything in the implemention
                // so a stub method gets created (which returns null)
                // MemoryStream a = x.ActuallyRetunsMemoryStream();
                // Assert.Null(a);

                // the clientinterface is more restricted than the implementation
                // but that's ok.
                MemoryStream ms = new MemoryStream();
                Assert.True(x.TakesAStream(ms));

                // the clientinterface is less restrictive than the implementation
                // and that's not ok.
                Assert.False(x.TakesAFileStream(ms));

                var shouldWork = new {
                    TakesAStream = new Func<Stream, bool>(stream => {return stream != null;})
                }.As<ClientInterface>();

                Assert.True(shouldWork.TakesAStream(ms));

                var shouldNotWork = new {
                    TakesAFileStream = new Func<MemoryStream, bool>(stream => {
                        Console.WriteLine("never called");
                        return stream != null;
                    })
                }.As<ClientInterface>();

                Assert.False(shouldWork.TakesAFileStream(ms));

                var shouldWorkToo = new {
                    ActuallyReturnsString = new Func<object>(() => "hello")
                }.As<ClientInterface>();

                Assert.NotNull(shouldWorkToo.ActuallyReturnsString());

                var shouldNotWorkToo = new {
                    ActuallyRetunsMemoryStream = new Func<Stream>(() => new MemoryStream())
                }.As<ClientInterface>();

                Assert.Null(shouldNotWorkToo.ActuallyRetunsMemoryStream());

                Func<object> fReturnsAString = new Func<object>(() => "hello");

                var fShouldWork = fReturnsAString.As<ReturnsAnObject>();

                Assert.NotNull(fShouldWork());

                Assert.Throws<Exception>(() => {
                    // this shouldn't work because the return type object
                    // can't be expressed as a string.
                    var fShouldNotWork = fReturnsAString.As<ReturnsAString>();
                });
            }
        }
Example #14
0
 public RpcHandler(Func <TInput, object?, CancellationToken, Task <TOutput> > handler, out string inputMessageName)
     : this(handler.As <MulticastDelegate>(), out inputMessageName)
 {
 }