首先,我知道标题不是很好,但事实是我甚至不知道如何解释我的问题;我将在下面展示一个我想要做的事例:类型推断问题:非泛型方法的泛型方法参数
编辑:我应该给出一个更好的例子来开始;让我们再试一次:
// MyAppComponentModel.dll
namespace MyAppComponentModel {
using System.Collections;
interface IResource { }
interface IStringResource : IResource { }
interface IIconResource : IResource { }
interface IDialogResource : IResource { }
interface IResourceProvider {
void GetResource<T>(out T result, IDictionary criteria = null) where T : IResource;
}
}
// ThirdPartyLib.dll
namespace ResourceProviderLibA {
using System.Collections;
using System.ComponentModel.Composition;
using MyAppComponentModel.
public sealed class StringResource : IStringResource { ... }
public sealed class IconResource : IIconResource { ... }
[Export(typeof(IResourceProvider))]
public sealed class StringAndIconResourceProvider : IResourceProvider {
void IResourceProvider.Get<T>(out T result, IDictionary criteria) {
if (typeof(T) == typeof(IDialogResource))
throw new NotSupportedException();
this.InternalGet(out result, criteria);
}
void InternalGet(out IStringResource result, IDictionary criteria) {
result = new StringResource();
...
}
void InternalGet(out IIconResource result, IDictionary criteria) {
result = new IconResource();
...
}
}
}
// MyMefTestApp.exe
namespace MyMefTestApp {
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using MyAppComponentModel.
static class Program {
[ImportMany(typeof(IResourceProvider))]
private IEnumerable<IResourceProvider> mProviders;
static void Main(String[] args) {
foreach (var provider in this.mProviders) {
...
}
}
}
}
我知道这是在某种程度上可能的,我坚信我做了这样的事情曾经只是不记得如何。任何人?
我已经知道这可以通过反射完成,所以请跳过这些解决方案 - 谢谢。
我已将示例代码更新为更实用的示例;请看看它,看看它是否以任何方式改变你的答案。我真的需要一些帮助,或者至少有一个“良好的编程”方式来解决这个问题。 – gplusplus 2011-02-13 00:39:58