是否有可能在運行時基於某些指定類型創建一個一般類型的Action?在這個特定的場景中,Action的主體最終會忽略參數類型,因爲類型化的Action只會是一個無參數Action的包裝,在運行時創建通用類型的Action <>
Action original =() => { };
...
Action<TType> wrapper = (arg) => {
original();
}
或者,甚至:
Action<TTypeA, TTypeB> wrapper = (arg) => {
original();
}
正如你所看到的,類型化的行動<>正文忽略的參數,和它們的類型,它只是作爲一個包裝。
如果您好奇我爲什麼要首先創建這個包裝器,那麼'基本'版本是我最終將Action轉換爲Delegate.Combine(),它需要相同的類型。我試圖用Delegate.Combine()完成的一個基本通知是委託人被解僱。
在這一點上,我可能會重新設計我的設計,以避免這些類型的惡作劇,但我仍然很好奇這是如何實現的。
我能得到的最接近的是以下幾點:
private static TType GetTypedDelegate<TType>(Action onComplete)
where TType : class
{
MethodInfo info = typeof(TType).GetMethod("Invoke");
ParameterInfo[] parameters = info.GetParameters();
object result;
if (parameters.Length == 0)
result = onComplete;
else if (parameters.Length == 1)
result = GetTypedDelegate<TType>(onComplete, parameters[0].ParameterType);
// etc
TType onCompleteCasted = Delegate.CreateDelegate(typeof(TType), result, "Invoke") as TType;
return onCompleteCasted;
}
private static Delegate GetTypedDelegate<TType>(Action onComplete, Type type)
{
// This line isn't useful for me right now, since I can't just create a new
// instance of the action with a parameterless constructor ... but I thought I'd throw it in here in case it was of use
Type actionType = typeof(Action<>).MakeGenericType(new[] { type });
// Do some magic here with the type information
// The following of course does not work,but you get the idea of what I am aiming for
Action<type> wrapper = (arg1) =>
{
onComplete();
};
return wrapper as Delegate;
}
這可能只是伎倆!現在嘗試一下。我沒有想到在返回期間讓隱式轉換髮生 – Matt