当前位置: 首页>>代码示例>>C#>>正文


C# Func.As方法代码示例

本文整理汇总了C#中Func.As方法的典型用法代码示例。如果您正苦于以下问题:C# Func.As方法的具体用法?C# Func.As怎么用?C# Func.As使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Func的用法示例。


在下文中一共展示了Func.As方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AddValidationRule

        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;
        }
开发者ID:fzhenmei,项目名称:Serenity,代码行数:14,代码来源:ValidationExtensions.cs

示例2: TestAsFunction

        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>()();
                });
            }
        }
开发者ID:notgerry,项目名称:oneget,代码行数:42,代码来源:DynamicInterfaceTest.cs

示例3: CheckForAcceptableTypes

        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>();
                });
            }
        }
开发者ID:notgerry,项目名称:oneget,代码行数:64,代码来源:DynamicInterfaceTest.cs


注:本文中的Func.As方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。