Class / Action<T> 的构造函数?
Class / constructor to Action<T>?
我有两个 class,它们都继承自 Stream class。
FirstClass : Stream
{
FirstClass(Stream, CompressionMode)
{
..
}
}
SecondClass : Stream
{
SecondClass(Stream, CompressionMode)
{
..
}
}
我想将其中一个 classes 放入一个变量(我猜是操作),然后从该变量创建一个新对象——像这样:
Action<Stream, CompressionMode> classPattern = condition ? FirstClass : SecondClass
///And now this
var newObject = classPattern(Stream, CompressionMode);
///Or maybe even this
classPattern newObject = classPattern(Stream, CompressionMode);
你好吗?
感谢帮助<3
看来,您需要一个带有两个参数的Func
delegate
Func<Stream, CompressionMode, Stream> classPattern = (stream, mode) => condition ? (Stream)new FirstClass(stream, mode) : new SecondClass(stream, mode);
并使用它
var newObject = classPattern(stream, compressionMode);
您也可以将其转换为本地函数
Stream ClassPattern(Stream stream, CompressionMode mode) =>
condition ? (Stream) new FirstClass(stream, mode) : new SecondClass(stream, mode);
我怀疑你是否真的想要这里的任何 lambda (Func<Stream, CompressionMode, Stream>
)。如果你有很多模式、条件、选项,labda 将太复杂而无法维护。我建议 factory class:
public static class MyClassBuilder {
public static Stream Create(Stream parent, CompressionMode mode) {
// Note, that we can easily include validation into factory's method
if (null == parent)
throw new ArgumentNullException(nameof(parent));
return condition(mode, parent)
? new FirstClass(parent, mode)
: new SecondClass(parent, mode);
}
// It's easy to implement building with default mode:
public static Stream Create(Stream parent, CompressionMode mode) =>
Create(parent, CompressionMode.MaximumCompression);
}
.....
var newObject = MyClassBuilder.Create(someStream, someCompressionMode);
// with default CompressionMode
var otherObject = MyClassBuilder.Create(someOtherStream);
我有两个 class,它们都继承自 Stream class。
FirstClass : Stream
{
FirstClass(Stream, CompressionMode)
{
..
}
}
SecondClass : Stream
{
SecondClass(Stream, CompressionMode)
{
..
}
}
我想将其中一个 classes 放入一个变量(我猜是操作),然后从该变量创建一个新对象——像这样:
Action<Stream, CompressionMode> classPattern = condition ? FirstClass : SecondClass
///And now this
var newObject = classPattern(Stream, CompressionMode);
///Or maybe even this
classPattern newObject = classPattern(Stream, CompressionMode);
你好吗?
感谢帮助<3
看来,您需要一个带有两个参数的Func
delegate
Func<Stream, CompressionMode, Stream> classPattern = (stream, mode) => condition ? (Stream)new FirstClass(stream, mode) : new SecondClass(stream, mode);
并使用它
var newObject = classPattern(stream, compressionMode);
您也可以将其转换为本地函数
Stream ClassPattern(Stream stream, CompressionMode mode) =>
condition ? (Stream) new FirstClass(stream, mode) : new SecondClass(stream, mode);
我怀疑你是否真的想要这里的任何 lambda (Func<Stream, CompressionMode, Stream>
)。如果你有很多模式、条件、选项,labda 将太复杂而无法维护。我建议 factory class:
public static class MyClassBuilder {
public static Stream Create(Stream parent, CompressionMode mode) {
// Note, that we can easily include validation into factory's method
if (null == parent)
throw new ArgumentNullException(nameof(parent));
return condition(mode, parent)
? new FirstClass(parent, mode)
: new SecondClass(parent, mode);
}
// It's easy to implement building with default mode:
public static Stream Create(Stream parent, CompressionMode mode) =>
Create(parent, CompressionMode.MaximumCompression);
}
.....
var newObject = MyClassBuilder.Create(someStream, someCompressionMode);
// with default CompressionMode
var otherObject = MyClassBuilder.Create(someOtherStream);