的precompiler指令在C#中摆脱 [英] Getting rid of precompiler directives in C#

查看:145
本文介绍了的precompiler指令在C#中摆脱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求保持一些不作为遗留-AS-I-会般的code,这是充满了编译器指令,使之成为pretty的多读,而几乎一样维护。案例分析:<​​/ P>

I've been asked to maintain some not-as-legacy-as-I-would-like code, and it is riddled with compiler directives, making it pretty much unreadable and almost as maintainable. Case in point:

#if CONDITION_1
        protected override void BeforeAdd(LogEntity entity)
#else
        protected override void BeforeAdd(AbstractBusinessEntity entity)
#endif
        {
#if CONDITON_1
            entity.DateTimeInsert = DateTime.Now;
#else
            ((LogEntity) entity).DateTimeInsert = DateTime.Now;
#endif
            base.BeforeAdd(entity);
        }

使用指令甚至是prettier:

using directives are even prettier:

#if CONDITION_1
using CompanyName.Configuration;
#endif

#if CONDITION_2||CONDITION_1
using CompanyName.Data;
using CompanyName.Data.SqlBuilders;
#else
using CompanyName.Legacy.Database;
using CompanyName.Legacy.Database.SQLBuilders;
using CompanyName.Legacy.Database.SQLBuilders.parameterTypes;
#endif

我想我给了 ConditionalAttribute 一展身手,但不会很工作在这种情况下

I thought I'd give the ConditionalAttribute a go but that won't quite work in this situation

有什么办法,我可以工作,我的出路,这个编译器指令噩梦?

Is there any way I can work my way out of this compiler directive nightmare?

在code编译反对 .NET 3.5

更新:
奥德答道建议删除周围的 BeforeAdd 方法,因此超载的编译器指令。不幸的是这是行不通的,因为这两种方法都应该是压倒一切的 AbstractBusiness 类,它提供取决于哪个组件最终被列入两种不同的实现方式:

UPDATE:
Oded answered suggesting removing the compiler directives around the BeforeAdd method thus overloading it. Unfortunately that won't work since both methods are supposed to be overriding an AbstractBusiness class which provides two different implementations depending on which assemblies end up being included:

protected virtual void BeforeAdd(TEntity entity) {}

protected virtual void BeforeAdd(AbstractBusinessEntity entity) {}

这code获取其相关性从一组库德公司创造一定的时间过去,从那时起已经升级换代。他们现在有一个组库的4碰撞命名空间和不同的实现不同的版本。所有在向后兼容的名称与使用(很)旧版本的应用程序。

This code gets its dependencies from a set of libraries de company created some time in the past and have been "upgrading" ever since. They now have 4 different versions of that set of libraries with colliding namespaces and differing implementations. All in the name of "backwards compatibility" with applications that use the (very) old versions.

我最终选择@奥德的答案,因为它是最有意义的作为一般的做法( KISS 和所有)。在这种情况下,虽然我不能用它;你在这里看到的是冰山的一角。我不想K.I.S.S.如果付给我这个code。

I ended up choosing @Oded's answer because it makes the most sense as a general approach (K.I.S.S. and all that). I could not use it in this case though; what you see here is just the tip of the iceberg. I wouldn't want to K.I.S.S. this code if it paid me.

推荐答案

在第一种情况下,它看起来像你可以简单地有方法,而不是这种结构的几个重载。重载应该注意的事情,在这一点上。

In the first case, it looks like you could simply have several overloads of the method instead of this construct. Overload resolution should take care of things at this point.

在第二种情况下(使用指令) - 你可引用一些指令,包括所有的人,用在需要的地方别名。当包括所有的命名空间,会发生什么?任何名称冲突?

In the second case (using directives) - you can alias some of the directives and include all of them, using the alias where needed. What happens when all namespaces are included? Any name collisions?

这篇关于的precompiler指令在C#中摆脱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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