ASp.net vnext中的AssemblyNeutral属性

编程入门 行业动态 更新时间:2024-10-28 18:27:27
本文介绍了ASp vnext中的AssemblyNeutral属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近遇到了ASP vnext的 AssemblyNeutral 属性.

I recently come across AssemblyNeutral attribute of ASP vnext.

如果需要具体说明,那么您将在记录"存储库中找到该属性的用法. github/aspnet/logging

If I have to be specific then you will find that attribute usage in Logging respository. github/aspnet/logging

还有许多其他存储库,但这很简单.

There are many other repository but this is simple one.

现在,根据许多博客,如果我们使用 AssemblyNeutral 属性,则该接口或类型不与程序集绑定,因此我们可以在其他框架中使用 AssemblyNeutral 属性定义相同的接口,想要相同的合同.

Now as per many blog if we use AssemblyNeutral attribute then that interface or type not tied with assembly so we can defined same interface with AssemblyNeutral attribute in some other framework that want same contract.

所以创建示例时,我已经在ASP vnext类库项目中创建了自己的小型记录器库,但是它给了我类型转换错误.

So create example I have created my own small logger library in ASP vnext class library project but It gives me type cast error.

namespace MyLogger { #if ASPNET50 || ASPNETCORE50 [Microsoft.Framework.Runtime.AssemblyNeutral] #endif public interface ILogger { void Write(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter); bool IsEnabled(LogLevel logLevel); IDisposable BeginScope(object state); } #if ASPNET50 || ASPNETCORE50 [Microsoft.Framework.Runtime.AssemblyNeutral] #endif public interface ILoggerFactory { ILogger Create(string name); void AddProvider(ILoggerProvider provider); } #if ASPNET50 || ASPNETCORE50 [Microsoft.Framework.Runtime.AssemblyNeutral] #endif public interface ILoggerProvider { ILogger Create(string name); } #if ASPNET50 || ASPNETCORE50 [Microsoft.Framework.Runtime.AssemblyNeutral] #endif public enum LogLevel { Verbose = 1, Information = 2, Warning = 3, Error = 4, Critical = 5, } public class MyLogger : ILogger { public IDisposable BeginScope(object state) { return null; } public bool IsEnabled(LogLevel logLevel) { return true; } public void Write(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter) { System.IO.File.AppendAllText("C:\\Testlog.txt", "This is test log" + System.Environment.NewLine); } } public class MyLoggerProvider : ILoggerProvider { private readonly Func<string, LogLevel, bool> _filter; public MyLoggerProvider(Func<string, LogLevel, bool> filter) { _filter = filter; } public ILogger Create(string name) { return new MyLogger(); } } public static class MyLoggerExtentions { public static ILoggerFactory AddMyLogger(this ILoggerFactory factory, Func<string, LogLevel, bool> filter) { factory.AddProvider(new MyLoggerProvider(filter)); return factory; } } }

现在,我正在尝试在SampleApp(日志存储库SampleApp)中使用.您会看到它给了我Cast错误.为什么这样 ?

Now I am trying to use in SampleApp ( Logging Repository SampleApp). You can see that it gives me Cast Error. Why so ?

推荐答案

我部分同意Visual Studio 2015预览版IDE不支持AssemlbyNeutral属性,但问题出在其他地方.

I am partially agree with Visual Studio 2015 Preview IDE does not support AssemlbyNeutral attribute but Problem is somewhere else.

实际上,当我们在自定义程序集中再次定义Interface或Type时,如果我们希望它支持AssemblyNeutral,则它必须具有与原始接口相同的名称空间.

Actually when we defining Interface or Type again in custom assembly and if we want it support AssemblyNeutral then it must have same namespace as original interface.

在我的示例中,我通过将接口放在New Namespace中来做错了.

In my sample I have done it wrong by putting interface in New Namespace.

我已更新代码,然后在Visual Studio 2015预览版中构建它可以编译甚至正确执行.

I have updated the code and then I build in Visual Studio 2015 Preview It compile and even execute correctly.

因此要记住的主要事情是放置程序集中性接口和类型,使其与原始程序集所驻留的名称空间相同."

So main thing to remember is "Put Assembly Neutral Interface and types in same namespace as it resides for original assembly".

以下是更新的代码.

namespace Microsoft.Framework.Logging { #if ASPNET50 || ASPNETCORE50 [Microsoft.Framework.Runtime.AssemblyNeutral] #endif public interface ILogger { void Write(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter); bool IsEnabled(LogLevel logLevel); IDisposable BeginScope(object state); } #if ASPNET50 || ASPNETCORE50 [Microsoft.Framework.Runtime.AssemblyNeutral] #endif public interface ILoggerFactory { ILogger Create(string name); void AddProvider(ILoggerProvider provider); } #if ASPNET50 || ASPNETCORE50 [Microsoft.Framework.Runtime.AssemblyNeutral] #endif public interface ILoggerProvider { ILogger Create(string name); } #if ASPNET50 || ASPNETCORE50 [Microsoft.Framework.Runtime.AssemblyNeutral] #endif public enum LogLevel { Verbose = 1, Information = 2, Warning = 3, Error = 4, Critical = 5, } } namespace MyLogger { public class MyLogger : ILogger { public IDisposable BeginScope(object state) { return null; } public bool IsEnabled(LogLevel logLevel) { return true; } public void Write(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter) { System.IO.File.AppendAllText("C:\\Testlog.txt", "This is test log" + System.Environment.NewLine); } } public class MyLoggerProvider : ILoggerProvider { private readonly Func<string, LogLevel, bool> _filter; public MyLoggerProvider(Func<string, LogLevel, bool> filter) { _filter = filter; } public ILogger Create(string name) { return new MyLogger(); } } public static class MyLoggerExtentions { public static ILoggerFactory AddMyLogger(this ILoggerFactory factory, Func<string, LogLevel, bool> filter) { factory.AddProvider(new MyLoggerProvider(filter)); return factory; } } }

更多推荐

ASp.net vnext中的AssemblyNeutral属性

本文发布于:2023-06-10 16:24:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/614987.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   net   ASp   AssemblyNeutral   vnext

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!