小编Fab*_*ied的帖子

"静态类导入"实际上是C#的未来特征的宣布或暗示吗?

这个编辑开始,关于C#维基百科文章提到了"静态类导入"作为C#的未来特性,以及"编译器即服务("Roslyn")".

这个功能是否已经由编译团队的成员实际宣布或暗示过,或者这只是一个疯狂的推测?

c# static-import

6
推荐指数
1
解决办法
148
查看次数

如何在具有AllowPartiallyTrustedCallersAttribute的库程序集中的.NET 4中实现Exception.GetObjectData?

我有一个标有AllowPartiallyTrustedCallersAttribute包含自定义异常类的程序集.我想通过覆盖使其可序列化GetObjectData.

使用.NET 4,GetObjectData已经成为一种SecurityCritical方法.这意味着还需要覆盖SecurityCritical.由于我的程序集标有AllowPartiallyTrustedCallersAttribute,所以SecurityTransparent除非另有说明,否则所有代码都是自动的.因此,我将应用于SecurityCriticalAttributeGetObjectData覆盖:

using System;
using System.Runtime.Serialization;
using System.Security;

[assembly:AllowPartiallyTrustedCallers]

namespace Library
{
  [Serializable]
  public class MyException : Exception
  {
    public string String;

    public MyException ()
    {
    }

    protected MyException (SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
      String = info.GetString ("String");
    }

    [SecurityCritical]
    public override void GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
    {
      info.AddValue ("String", String);
      base.GetObjectData (info, context);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这在完全信任的情况下工作正常,例如,当我运行从桌面链接此程序集的代码时.

但是,当我从安全沙箱中使用这个类时(见下文),我得到了一个 …

c# code-access-security

6
推荐指数
1
解决办法
4665
查看次数

自定义属性类是否继承"Inherited"AttributeUsage标志?

请考虑以下情形:

  • 甲基属性类BaseAttribute具有AttributeUsageAttribute指定它不是可继承(Inherited = False).
  • 派生属性类DerivedAttribute继承自该基本属性类.
  • 基本域类Base已应用派生属性.
  • Derived要求从基域类继承的域类具有其自定义属性,包括继承的属性(inherit: true).

这是相应的代码:

using System;
using System.Linq;

namespace ConsoleApplication26
{
  class Program
  {
    static void Main ()
    {
      var attributes = typeof (Derived).GetCustomAttributes (true);
      foreach (var attribute in attributes)
      {
        Console.WriteLine (
            "{0}: Inherited = {1}",
            attribute.GetType().Name,
            attribute.GetType().GetCustomAttributes (typeof (AttributeUsageAttribute), true).Cast<AttributeUsageAttribute>().Single().Inherited);
      }
    }
  }

  [AttributeUsage (AttributeTargets.All, Inherited = false)]
  public class BaseAttribute : Attribute
  {
  }

  public class DerivedAttribute : BaseAttribute …
Run Code Online (Sandbox Code Playgroud)

c# reflection custom-attributes

6
推荐指数
1
解决办法
1690
查看次数

为什么进程会在 RtlExitUserProcess/LdrpDrainWorkQueue 中挂起?

为了调试锁定的文件问题,我们从 .NET 进程调用 SysInternal 的 Handle64.exe 4.11(通过Process.Start异步输出重定向)。调用进程挂起,Process.WaitForExit因为 Handle64 进程没有退出(超过两个小时)。

我们转储了相应的 Handle64 进程,并在 Visual Studio 2017 调试器中检查了它。它显示两个线程(“主线程”和“ntdll.dll!TppWorkerThread”)。

主线程的调用栈:

ntdll.dll!NtWaitForSingleObject ()  Unknown
ntdll.dll!LdrpDrainWorkQueue()  Unknown
ntdll.dll!RtlExitUserProcess()  Unknown
kernel32.dll!ExitProcessImplementation  ()  Unknown
handle64.exe!000000014000664c() Unknown
handle64.exe!00000001400082a5() Unknown
kernel32.dll!BaseThreadInitThunk    ()  Unknown
ntdll.dll!RtlUserThreadStart    ()  Unknown
Run Code Online (Sandbox Code Playgroud)

工作线程的调用堆栈:

ntdll.dll!NtWaitForSingleObject()   Unknown
ntdll.dll!LdrpDrainWorkQueue()  Unknown
ntdll.dll!LdrpInitializeThread()    Unknown
ntdll.dll!_LdrpInitialize() Unknown
ntdll.dll!LdrInitializeThunk()  Unknown
Run Code Online (Sandbox Code Playgroud)

我的问题是:为什么进程会挂起LdrpDrainWorkQueue?从/sf/answers/2995277911/,我了解到这是正在工作的 Windows 10 并行加载器,但为什么它会在退出进程时卡住呢?这是否是由于我们从另一个进程调用 Handle64 的方式引起的?即,我们是否做错了什么,或者这是 Handle64 中的一个错误?

c++ windows multithreading process

5
推荐指数
1
解决办法
2769
查看次数

AssemblyName.ReferenceMatchesDefinition 如何工作?

给出以下代码:

  var n1 = new AssemblyName ("TestDll, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089");
  var n2 = new AssemblyName ("TestDll, Version=2.0.0.2001, Culture=en-US, PublicKeyToken=ab7a5c561934e089");

  Console.WriteLine (AssemblyName.ReferenceMatchesDefinition (n1, n2));
  Console.WriteLine (AssemblyName.ReferenceMatchesDefinition (n2, n1));
Run Code Online (Sandbox Code Playgroud)

为什么这两个检查都打印“True”?我本以为 AssemblyName.ReferenceMatchesDefinition 应该考虑程序集名称的版本、区域性和公钥标记属性的差异,不是吗?

如果没有,ReferenceMatchesDefinition 能做什么而简单名称的比较却不能做什么?

c# reflection assembly-resolution assembly-name

4
推荐指数
1
解决办法
723
查看次数

ASP.NET Core MVC标记帮助程序参数中的字符串文字

Razor允许将ASP.NET Core MVC标记助手的参数写为相应属性声明中的内联C#表达式。但是,由于HTML属性由引号分隔,如果这样的表达式本身应包含引号,则语法是什么?

这是来自https://docs.microsoft.com/zh-cn/aspnet/core/mvc/views/tag-helpers/authoring的示例:

<website-information info="new WebsiteContext {
    Version = new Version(1, 3),
    CopyrightYear = 1638,
    Approved = true,
    TagsToShow = 131 }" />
Run Code Online (Sandbox Code Playgroud)

如果其中一个WebsiteContext属性采用字符串文字,那会是什么样?

c# razor asp.net-core asp.net-core-tag-helpers

3
推荐指数
2
解决办法
934
查看次数

如何在特定的线程文化中执行MSpec规范?

我有一些测试代码执行文化感知转换的规范.我想为我的测试设置一个定义的文化,这样我就可以对预期的值进行硬编码,而不必担心运行测试的系统的配置文化.

有没有一种简单的方法可以使用Machine.Specifications来执行此操作,还是必须设置Thread.CurrentThread.CurrentCulture(也可能CurrentUICulture)?

cultureinfo currentculture mspec

1
推荐指数
1
解决办法
159
查看次数