您将如何创建一种扩展方法,使我能够执行以下操作(警告:exteme伪代码)...
class FooBar
{
Int32 Foo { get; set; }
String Bar { get; set; }
}
new FooBar().With(fb => new Func<FooBar, Object>(instance =>
{
// VB With magic
// NOTE: The instance parameter HAS to be by reference
instance.Foo = 10;
instance.Bar;
return new Object();
}));
如果您可以指定没有返回类型(void)的匿名函数,那么上面的代码看起来会更干净...
new FooBar().With(fb => new Func<FooBar, void>(instance =>
{
instance.Foo = 10;
instance.Bar;
}));
这是最糟糕的伪代码。但我希望你能明白。
最佳答案
也许这会有所帮助:
new FooBar().With( fb=> {
fb.Foo = 10;
fb.Bar = fb.Foo.ToString();
} );
// ... somewhere else ...
public static void With<T>( this T target, Action<T> action ) {
action( target );
}
关于c# - 在C#中实现VB With语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3634214/