要检查是否存在特定的项目引用(C#代码,预处理程序指令...) [英] To check wether a specific project reference exists (C# Code, Preprocessor Directives ...)

查看:92
本文介绍了要检查是否存在特定的项目引用(C#代码,预处理程序指令...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#/ .NET还是很陌生,所以我希望有一个解决我问题的方法。

i'm fairly new to C# / .NET so I hope there is a solution for my problem.

我正在使用2个不同的图像处理库它们有自己的类来表示图像。因为我不想在自己的(私有)类库中使用这些类,所以我想实现自己的 myImage -Class,该类应该作为其他两个类的包装。 (如果缺少它们,我当然要添加这些库的功能。)

I'm working with 2 different image-processing libraries which have their own classes to represent images. Because I don't want to use this classes in my own (private) class library, I want to implement my own "myImage"-Class which is supposed to be a wrapper around both of the other classes. (I have of course to add the functionality of the libraries if they are missing).

我的问题现在基本上是这样的:myImage类需要同时引用这两个类库进行编译,但是我有时想在只有一个库可用的项目中使用myImage类。我发现添加或删除对图像类项目的引用并不困难,但是我不想注释掉缺少库的代码。

My problem is basically now like this: The myImage-class needs a reference to both libraries to compile, however i sometimes would like to use the myImage-class in projects where only one library is available. I find it not difficult to "add" or "remove" references to my image class project, however i don't want to comment out the code which refers to the missing library.

所以我的问题是:是否可以通过预处理指令或属性或代码直接确定是否设置了引用?还是有更好的解决方案(也许是聪明的班级设计?)

So my question is: is it somehow possible to determine wether a reference is set with a preprocessing directive or with attributes or with code directly? Or is there even a better solution for my problem (a clever class design maybe?)

感谢帮助

推荐答案

我认为没有预处理器指令可以告诉您是否添加了引用。

I don't think there's a preprocessor directive that will tell you if a reference has been added.

将您的班级分成两个单独的班级,每个库一个班级,但是我怀疑您的意图是将这两个班级合并为一个统一班级。

You could always split your class into two separate classes, one for each library, but I suspect your intention was to join the two into one unified class.

我要做的是使用2条件编译符号(请参阅属性|生成),一个用于第一个库的特定方法,另一个用于第二个库。每当类的代码引用特定于库的数据成员或函数时,它就会被#if指令包围,并引用相应的符号。

What I would do is use 2 conditional compilation symbols (see Properties | Build), one for methods specific to the first library, the other for the second library. Whenever the class's code refers to data members or functions specific the the library, it would be surrounded by a #if directive, referring to the appropriate symbol.

如果您的意图是不参加课程,里德和丹尼尔的解决方案可能更合适。

If your intention was not to join the classes, Reed's and Daniel's solutions are probably more appropriate.

这篇关于要检查是否存在特定的项目引用(C#代码,预处理程序指令...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆