【问题标题】:Managing enum types among configurations of assemblies管理程序集配置之间的枚举类型
【发布时间】:2017-05-08 08:49:31
【问题描述】:

源代码是 C# 并且有很多遗留代码。我使用带有 GUI 的 Winforms。

我使用源代码构建了一个程序集,但该程序集包含在两个不同的包中。第一个包是每个人都可以下载的免费演示应用程序(FApp),第二个包是非免费且更专业的应用程序(ProApp)。所以你明白这两个包没有相同的功能集。

在 ProApp 中定义了一个枚举类型。我有 FApp 的问题,因为并非枚举中的所有值都是相关的,它们不能以 GUI 的形式出现。这就是为什么我坚持条件编译的原因。但它会导致使用枚举的方法出现编译错误。枚举用于许多代码行。现在我不确定这是个好主意。

原代码:

public enum eCars {Toyota, Honda, Hyundai, BMW, Acura};

我不太好的解决方案: #if 应用程序 公共枚举 eCars {丰田、本田、现代}; #别的 公共枚举 eCars {丰田、本田、现代、宝马、讴歌}; #endif

就像我说的,使用枚举 eCars 的方法中有很多编译错误。

public static bool IsItHyundai(eCars car)
{
    if (car == eCars.Hyundai)
        return true;

    return false;
}

您能否提出其他解决方案。

【问题讨论】:

    标签: c# winforms enums compilation conditional


    【解决方案1】:

    我的建议是在 FApp 中定义 Enum,并让 ProApp 使用与 FApp 相同的 Enum。然后,您可以在控制使用和行为的枚举上使用属性。然后可以使用读取装饰每个 ENUM 值的属性的扩展方法来指示行为。

    以下是一些实现示例代码:

    public class eCarsUsageAttribute : Attribute
        {
            public eCarsUsageAttribute() { }
            public eCarsUsageAttribute(bool allowInFApp = true)
            {
                AllowInFApp = allowInFApp;
            }
            public bool AllowInFApp { get; set; }
        }
        public enum eCars
        {
            [eCarsUsageAttribute]
            Toyota,
            [eCarsUsageAttribute]
            Honda,
            [eCarsUsageAttribute(false)]
            Hyundai,
            [eCarsUsageAttribute]
            BMW,
            [eCarsUsageAttribute]
            Acura
        };
        public static class EnumExtensions
        {
            public static bool AllowInFreeApp(this eCars value)
            {
                lock (_usageValues)
                {
                    //reflection is somewhat expensive so I'd recommend using a local store to keep the attributes that you have already looked up
                    if (!_usageValues.ContainsKey(value))
                    {
                        // Get the type
                        Type type = value.GetType();
    
                        // Get fieldinfo for this type
                        System.Reflection.FieldInfo fieldInfo = type.GetField(value.ToString());
    
                        // Get the stringvalue attributes
                        eCarsUsageAttribute[] attribs = fieldInfo.GetCustomAttributes(typeof(eCarsUsageAttribute), false) as eCarsUsageAttribute[];
    
                        var attr = attribs.FirstOrDefault();
                        if (attr != null)
                            _usageValues[value] = attr.AllowInFApp;
                        else
                            _usageValues[value] = false;//Depends on what you want the default behavior to be
    
                    }
                    return _usageValues[value];
                }
            }
            private static Dictionary<eCars, bool> _usageValues = new Dictionary<eCars, bool>();
        }
    

    【讨论】:

    • 但是如何定义 IsItHyundai 方法?
    • @Smurf 该方法的实现不会改变。 Enum 值将存在于两个应用程序环境中,因为它们将共享一个实现——因此编译器不会有问题。需要对应用程序的其他部分进行更改,以防止使用标记为不可用的 eCar。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    相关资源
    最近更新 更多