我有一个标有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)
这在完全信任的情况下工作正常,例如,当我运行从桌面链接此程序集的代码时.
但是,当我从安全沙箱中使用这个类时(见下文),我得到了一个 …
请考虑以下情形:
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) 为了调试锁定的文件问题,我们从 .NET 进程调用 SysInternal 的 Handle64.exe 4.11(通过Process.Start异步输出重定向)。调用进程挂起,Process.WaitForExit因为 Handle64 进程没有退出(超过两个小时)。
我们转储了相应的 Handle64 进程,并在 Visual Studio 2017 调试器中检查了它。它显示两个线程(“主线程”和“ntdll.dll!TppWorkerThread”)。
主线程的调用栈:
Run Code Online (Sandbox Code Playgroud)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
我的问题是:为什么进程会挂起LdrpDrainWorkQueue?从/sf/answers/2995277911/,我了解到这是正在工作的 Windows 10 并行加载器,但为什么它会在退出进程时卡住呢?这是否是由于我们从另一个进程调用 Handle64 的方式引起的?即,我们是否做错了什么,或者这是 Handle64 中的一个错误?
给出以下代码:
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 能做什么而简单名称的比较却不能做什么?
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属性采用字符串文字,那会是什么样?
我有一些测试代码执行文化感知转换的规范.我想为我的测试设置一个定义的文化,这样我就可以对预期的值进行硬编码,而不必担心运行测试的系统的配置文化.
有没有一种简单的方法可以使用Machine.Specifications来执行此操作,还是必须设置Thread.CurrentThread.CurrentCulture(也可能CurrentUICulture)?
c# ×5
reflection ×2
asp.net-core ×1
c++ ×1
cultureinfo ×1
mspec ×1
process ×1
razor ×1
windows ×1