如何将通用 C# Action<T1> 转换为 Action<T2>
How to convert a generic C# Action<T1> to Action<T2>
我一直在用 C# 做一些工作来学习和创造一些东西;不幸的是,我是 C# 的新手,我在转换方面遇到了一些麻烦。
我有一个 Type1 的 Action,我想将它转换为 Type2;这两种类型在编译时都是已知的。这是我要归档的示例代码。
public class Example <Resolve, Reject>
{
protected Resolve resolved;
protected Reject rejected;
public Example( Resolve value ) { resolved = value; }
public Example( Reject value ) { rejected = value; }
public void invoke( Action<Resolve> callback ) {
if( null != resolved ) { callback (resolved ); }
else if( null != rejected ) {
// How to cast action from Action <Resolve> to Action <Rejected>
// and invoke it with the rejected value??
callback ( rejected );
}
else throw new ApplicationException( "Not constructed" );
}
}
public static void Main (string[] args)
{
Console.WriteLine ("Start");
var example1 = new Example <string, System.ArgumentException> ( "Str argument" );
example1.invoke (msg => {
// Here the msg is a string ok!
if (msg is string) { Console.WriteLine (msg); }
else { Console.WriteLine ("Exception"); }
});
var example2 = new Example <string, System.ArgumentException> ( new ArgumentException("An exception") );
example2.invoke (msg => {
// Here msg should be an ArgumentException.
if (msg is string) { Console.WriteLine (msg); }
else { Console.WriteLine ("Exception"); }
});
Console.WriteLine ("Done");
Console.ReadLine ();
}
我无法控制类型,所以我不能将一个类型转换为另一个类型,即使我可以,我正在实施的规范要求我必须使用 Resolve 或 Reject 值来解析回调,具体取决于在 运行 时间内发生的某些情况下。
你能帮帮我吗?
免责声明:我是以下开源库的作者
如果您使用 Succinc<T> 提供的区分联合类型和模式匹配,您可以简单地在此处编写您需要的内容:
public static void MessageOrException(Union<string, ArgumentException> value)
{
value.Match().Case1().Do(Console.WriteLine)
.Else(Console.WriteLine("Exception")
.Exec();
}
public static void Main (string[] args)
{
Console.WriteLine ("Start");
var example1 = new Union<string, ArgumentException> ("Str argument");
MessageOrException(example1); // Writes "Str argument"
var example2 =
new Union<string, ArgumentException>(new ArgumentException("An exception"));
MessageOrException(example2); // Writes "Exception"
Console.WriteLine ("Done");
Console.ReadLine ();
}
我一直在用 C# 做一些工作来学习和创造一些东西;不幸的是,我是 C# 的新手,我在转换方面遇到了一些麻烦。
我有一个 Type1 的 Action,我想将它转换为 Type2;这两种类型在编译时都是已知的。这是我要归档的示例代码。
public class Example <Resolve, Reject>
{
protected Resolve resolved;
protected Reject rejected;
public Example( Resolve value ) { resolved = value; }
public Example( Reject value ) { rejected = value; }
public void invoke( Action<Resolve> callback ) {
if( null != resolved ) { callback (resolved ); }
else if( null != rejected ) {
// How to cast action from Action <Resolve> to Action <Rejected>
// and invoke it with the rejected value??
callback ( rejected );
}
else throw new ApplicationException( "Not constructed" );
}
}
public static void Main (string[] args)
{
Console.WriteLine ("Start");
var example1 = new Example <string, System.ArgumentException> ( "Str argument" );
example1.invoke (msg => {
// Here the msg is a string ok!
if (msg is string) { Console.WriteLine (msg); }
else { Console.WriteLine ("Exception"); }
});
var example2 = new Example <string, System.ArgumentException> ( new ArgumentException("An exception") );
example2.invoke (msg => {
// Here msg should be an ArgumentException.
if (msg is string) { Console.WriteLine (msg); }
else { Console.WriteLine ("Exception"); }
});
Console.WriteLine ("Done");
Console.ReadLine ();
}
我无法控制类型,所以我不能将一个类型转换为另一个类型,即使我可以,我正在实施的规范要求我必须使用 Resolve 或 Reject 值来解析回调,具体取决于在 运行 时间内发生的某些情况下。
你能帮帮我吗?
免责声明:我是以下开源库的作者
如果您使用 Succinc<T> 提供的区分联合类型和模式匹配,您可以简单地在此处编写您需要的内容:
public static void MessageOrException(Union<string, ArgumentException> value)
{
value.Match().Case1().Do(Console.WriteLine)
.Else(Console.WriteLine("Exception")
.Exec();
}
public static void Main (string[] args)
{
Console.WriteLine ("Start");
var example1 = new Union<string, ArgumentException> ("Str argument");
MessageOrException(example1); // Writes "Str argument"
var example2 =
new Union<string, ArgumentException>(new ArgumentException("An exception"));
MessageOrException(example2); // Writes "Exception"
Console.WriteLine ("Done");
Console.ReadLine ();
}