【发布时间】:2015-07-06 19:53:27
【问题描述】:
我一直在用 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 值来解决回调,具体取决于某些运行时发生的情况。
你能帮帮我吗?
【问题讨论】:
-
看来你真的需要传入两个单独的动作。
-
你在这里描述的是一个有区别的联合。正如@juharr 所说,您需要根据值是
T1还是T2类型来调用两个单独的操作。看看github.com/DavidArno/SuccincT/wiki/UnionT1T2,它是一个联合类型,支持通过模式匹配调用动作,在 Succinc库中。你可能会发现它提供了你需要的东西。 (免责声明:我写的)
标签: c# generics casting delegates