根据类的存在有条件地包含代码

发布于 2024-10-11 22:17:29 字数 457 浏览 1 评论 0原文

我有两个函数以两种不同的方式执行完全相同的操作。每个都使用不同的类来完成任务:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过潦 2024-10-18 22:17:29

通常,通过控制反转可以更好地处理此类情况,而不是尝试确定是否存在班级。

与其寻找“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 of IInterfaceB 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#.

2024-10-18 22:17:29

如果必须使用预处理器#if#define 指令,然后在 ClassA 添加

#define ClassA

在您的 HttpHandler 添加

#if ClassA
  void function() { ClassA a = new ClassA(); a.doWork(); }
#else
  void function() { ClassB b = new ClassB(); b.doSomething(); }
#endif

如果可能的话,最好使用 依赖注入< /a> 并将其传递到 HttpHandler 的构造函数中。

If you must do it with the preprocessor #if and #define directives, then in ClassA add

#define ClassA

In your HttpHandler add

#if ClassA
  void function() { ClassA a = new ClassA(); a.doWork(); }
#else
  void function() { ClassB b = new ClassB(); b.doSomething(); }
#endif

If possible it would be better to use dependency injection and pass it into the constructor of the HttpHandler.

悲凉≈ 2024-10-18 22:17:29

您可以使用 partial 关键字将您的类拆分为两个文件:

// MyClass.cs -- include this into every project
public partial class MyClass {
    public void functionB() { ClassB b = new ClassB(); b.doSomething(); }
}

// MyClass.functionA.cs -- include only into projects that have ClassA
public partial class MyClass {
    public void functionA() { ClassA a = new ClassA(); a.doWork(); }
}

我们在代码中的几个地方执行此操作。我喜欢“BaseName.Specialty.cs”命名约定,因为解决方案资源管理器会自动将 MyClass.functionA.cs 显示为嵌套在 MyClass.cs 下的子节点,与处理 .designer.cs 文件的方式相同。

You could split your class into two files using the partial keyword:

// MyClass.cs -- include this into every project
public partial class MyClass {
    public void functionB() { ClassB b = new ClassB(); b.doSomething(); }
}

// MyClass.functionA.cs -- include only into projects that have ClassA
public partial class MyClass {
    public void functionA() { ClassA a = new ClassA(); a.doWork(); }
}

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.

云胡 2024-10-18 22:17:29

您可以使用反射 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文