简体   繁体   English

使用指令预处理器处理多个目标框架

[英]Handle multiple target frameworks using directive preprocessor

I create a Net Standard class library targeting Net Standard 2.0 and Net Framework 4.6.2 to handle the two frameworks. 我创建了一个针对Net Standard 2.0Net Framework 4.6.2的Net Standard类库来处理这两个框架。 In Net Framework I need to distinguish if it's a Web API project or Web Application project. 在Net Framework中,我需要区分它是Web API项目还是Web Application项目。 This is what I want. 这就是我要的。

using System;
using System.Linq;

#if (netcoreapp2.0 || netcoreapp2.1)
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
#endif

//net462 || net47 || net471 || net472
#if (NETFX)
using System.Net.Http;
using System.Web;
using System.Web.Mvc;
#endif

namespace TestNetStandard
{
    //FOR NET MVC WEB
    public class ExceptionValidationAttribute : System.Web.Mvc.IExceptionFilter
    {
    }

    //FOR NET MVC API
    public class ExceptionValidationAttribute : System.Web.Http.Filters.ExceptionFilterAttribute
    {
    }

    //For NET CORE
    public class ExceptionValidationAttribute : Microsoft.AspNetCore.Mvc.Filters.ExceptionFilterAttribute
    {
    }
}

So, is it possible to create single class using preprocessor directive while the ExceptionValidationAttribute class inherits different classes? 那么,在ExceptionValidationAttribute类继承不同的类的同时,可以使用预处理器指令创建单个类吗? If yes, how to accomplish this scenario? 是,如何实现?

You can try something like this 您可以尝试这样的事情

#if ASPNETMVC
using ExceptionValidationAttributeParent = System.Web.Mvc.IExceptionFilter;
#elif ASPNETWEBAPI
using ExceptionValidationAttributeParent = System.Web.Http.Filters.ExceptionFilterAttribute;
#elif ASPNETCORE
using ExceptionValidationAttributeParent = Microsoft.AspNetCore.Mvc.Filters.ExceptionFilterAttribute;
#else
// default case but maybe redundant 
using ExceptionValidationAttributeParent = SomeFakeParentClass;
#endif



namespace TestNetStandard
{
    public class ExceptionValidationAttribute : ExceptionValidationAttributeParent
    {
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM