Are there any symbols when compiling a portable class library that can be used with preprocessor directives

笑着哭i 提交于 2019-12-24 12:08:30

问题


I have added a portable class library to my solution in which has a global file that is linked to all the projects. This global file contains the assembly level attributes such as version, culture, and Com visibility. The problem I want to solve is that some attributes such as ComVisible are not valid in the context of a PCL so is there any symbol that used with #if to prevent that attribute for being included at build time?


回答1:


I overcame this issue by adding stub/dummy attribute classes to a compact framework project that mimic their FCL counterparts:

// Shared source file
[ComVisible(true)]
public class Foo
{
}

// Dummy class in portable project -- exists only to facilitate compilation of the shared source file
namespace System
{
    [AttributeUsage(AttributeTarget.Class)]
    public class ComVisibleAttribute : Attribute
    {
        public ComVisibleAttribue(bool visible)
        {
            // Dummy implementation
        }
    }
}

Now the shared source code file will compile in both projects without any modification to the code, and without any preprocessor directives.




回答2:


You can configure your own pre-processor symbols within the build properties. I don't know if there are any specific ones by default for portable class libraries, but it wouldn't be hard to specify one in each configuration for your project.



来源:https://stackoverflow.com/questions/10194988/are-there-any-symbols-when-compiling-a-portable-class-library-that-can-be-used-w

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