Get a list of #define variables

为君一笑 提交于 2019-12-10 14:33:04

问题


Even though it should be impossible due to #define being a pre-processor directive I'd like to ask:

Is it possible to get a list of the #define'd variables within the actual program? Respectively, a list of conditional compilation symbols, defined within the project's properties.

Why would I need that? I'm managing extensions by using symbols. I'm trying to get a List of them to add them in my about window like

Enabled Extensions:
CUSTOMER1_ABC_EXTENSION
CUSTOMER2_XYZ_EXTENSION

Without writing specific code for each extension.


回答1:


It would appear that the cannonical answer is that you cannot do it. The MSDN documentation for #define says:

The scope of a symbol that was created by using #define is the file in which the symbol was defined.

This would suggest that you might be better off using reflection. Maybe you could use an Attribute class to decorate the extensions and provide run-time information that you can test for.




回答2:


Solved it using

public static class BuildVariables
{
    public static List<string> DefinedVariables = new List<string>()
    {
        #if CUSTOMER1_ABC_EXTENSION
        "CUSTOMER1_ABC_EXTENSION",
        #endif
        #if CUSTOMER2_XYZ_EXTENSION
        "CUSTOMER2_XYZ_EXTENSION",
        #endif
    };
}

Which is quite dirty and requires a change each time a new symbol is introduced. I don't like it.



来源:https://stackoverflow.com/questions/28187434/get-a-list-of-define-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!