根据类的存在有条件地包含代码
我有两个函数以两种不同的方式执行完全相同的操作。每个都使用不同的类来完成任务:
void functionA()
{
ClassA a = new ClassA();
a.doWork();
}
void functionB()
{
ClassB b = new ClassB();
b.doSomething();
}
这一切都在一个自包含的 HttpHandler 中,可以将其放入任何项目和工作中 - 至少这是目标。 FunctionA 将是首选的处理方式,FunctionB 是后备方案。 ClassA 在给定项目中可能不存在,ClassB 将始终存在。
有没有办法在运行时检测 ClassA 是否存在使用 functionA,如果不存在则使用 fcuntionB。如果 ClassA 不存在,还有办法编译它吗?我可以使用预处理器的东西来做到这一点(即我可以在编译时告诉要包含哪些内容吗?)
I have two function that do the exact same thing in two different ways. Each uses a different class to accomplish the task:
void functionA()
{
ClassA a = new ClassA();
a.doWork();
}
void functionB()
{
ClassB b = new ClassB();
b.doSomething();
}
this is all in a self contained HttpHandler that can be dropped in any project and work - At least thats the goal. FunctionA would be the preferred way of doing things, FunctionB is a fallback. ClassA might not exist in a given project, ClassB will always exist.
Is there a way at runtime to detect is ClassA exists an use functionA and if not use fcuntionB. Also is there a way to compile this if ClassA doesn't exist? Can I do this with pre-processor stuff (i.e. can I tell at compile time which to include?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常,通过控制反转可以更好地处理此类情况,而不是尝试确定是否存在班级。
与其寻找“ClassA”和“ClassB”,提供2个可以实现的接口将是更好的选择。然后,您可以使用 依赖注入 尝试注入
IInterfaceA
的实例,如果失败,请改用 IInterfaceB 实例。值得一读的好文章可能是 Martin Fowler 关于 IoC 的文章。另外,您可能需要查看托管扩展性框架(现已包含在 .NET 4 中),这使得这非常有用在 C# 中很容易。
Typically, situations like this are much better handled via Inversion of Control than trying to determine the existence of a class.
Instead of looking for "ClassA" and "ClassB", it would be a better option to provide 2 interfaces that could be implemented. You could then use Dependency Injection to attempt to inject an instance of
IInterfaceA
, and if it fails, use an instance ofIInterfaceB
instead.A good articles to read might be Martin Fowler's article on IoC. Also, you may want to look at the Managed Extensibility Framework (now included in .NET 4) which makes this very easy in C#.
如果必须使用预处理器#if 和 #define 指令,然后在 ClassA 添加
在您的
HttpHandler
添加如果可能的话,最好使用 依赖注入< /a> 并将其传递到
HttpHandler
的构造函数中。If you must do it with the preprocessor #if and #define directives, then in ClassA add
In your
HttpHandler
addIf possible it would be better to use dependency injection and pass it into the constructor of the
HttpHandler
.您可以使用
partial
关键字将您的类拆分为两个文件:我们在代码中的几个地方执行此操作。我喜欢“BaseName.Specialty.cs”命名约定,因为解决方案资源管理器会自动将 MyClass.functionA.cs 显示为嵌套在 MyClass.cs 下的子节点,与处理 .designer.cs 文件的方式相同。
You could split your class into two files using the
partial
keyword:We do this a few places in our code. I like the "BaseName.Specialty.cs" naming convention, because Solution Explorer will automatically show MyClass.functionA.cs as a sub-node nested under MyClass.cs, the same way it does with .designer.cs files.
您可以使用反射 (http://msdn. microsoft.com/en-us/library/f7ykdhsy(v=vs.71).aspx)。特别是 System.Reflection.Module 可以返回模块中可用类型的列表(http://msdn.microsoft.com/en-US/library/system.reflection.module.gettypes(v=VS.80).aspx) 。我想一旦你知道存在哪种类型,你就可以简单地进行强制转换。
You can use reflection (http://msdn.microsoft.com/en-us/library/f7ykdhsy(v=vs.71).aspx). Specifically System.Reflection.Module which can return a list of available types in the module (http://msdn.microsoft.com/en-US/library/system.reflection.module.gettypes(v=VS.80).aspx). I would imagine you can simply cast once you know which type exists.