我的程序通過Win32API函數與操作系統有很大的交互。現在我想將我的程序遷移到Linux下的Mono下運行(無酒),這需要與操作系統進行交互的不同實現。C#中針對平臺特定實現的統一
我開始設計一個代碼,可以針對不同的平臺實現不同的實現,並且可以爲新的未來平臺擴展。
public interface ISomeInterface
{
void SomePlatformSpecificOperation();
}
[PlatformSpecific(PlatformID.Unix)]
public class SomeImplementation : ISomeInterface
{
#region ISomeInterface Members
public void SomePlatformSpecificOperation()
{
Console.WriteLine("From SomeImplementation");
}
#endregion
}
public class PlatformSpecificAttribute : Attribute
{
private PlatformID _platform;
public PlatformSpecificAttribute(PlatformID platform)
{
_platform = platform;
}
public PlatformID Platform
{
get { return _platform; }
}
}
public static class PlatformSpecificUtils
{
public static IEnumerable<Type> GetImplementationTypes<T>()
{
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (Type type in assembly.GetTypes())
{
if (typeof(T).IsAssignableFrom(type) && type != typeof(T) && IsPlatformMatch(type))
{
yield return type;
}
}
}
}
private static bool IsPlatformMatch(Type type)
{
return GetPlatforms(type).Any(platform => platform == Environment.OSVersion.Platform);
}
private static IEnumerable<PlatformID> GetPlatforms(Type type)
{
return type.GetCustomAttributes(typeof(PlatformSpecificAttribute), false)
.Select(obj => ((PlatformSpecificAttribute)obj).Platform);
}
}
class Program
{
static void Main(string[] args)
{
Type first = PlatformSpecificUtils.GetImplementationTypes<ISomeInterface>().FirstOrDefault();
}
}
我看到兩個問題設計:
- 我不能強迫的
ISomeInterface
的實現有一個PlatformSpecificAttribute
。 - 多個實現可以標記爲相同的
PlatformID
,我不知道在Main中使用哪個。使用第一個是醜陋的。
如何解決這些問題?你能提出另一種設計嗎?
你不是在這裏。幾乎所有流行的DI容器都未通過MoMA測試。 Unity沒有通過所有的MoMA測試,但在某些情況下,它在Mono上運行良好。 – 2010-05-22 02:32:26