本文整理汇总了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;
}
示例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>()();
});
}
}
示例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>();
});
}
}