gpt4 book ai didi

c# - 如何防止 MemberInfo.IsDefined 在不相关的属性上抛出 FileNotFoundException?

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:53 24 4
gpt4 key购买 nike

我的项目引用类型为 SomeTypeTypesDefinitionAssembly,它由 XSerializationLibrary 中的属性 XSerializationOptions 标记>YSerializationOptions 来自 YSerializationLibrary

显然,要检查SomeType 是否由XSerializationOptions 标记,我还需要引用XSerializationLibrary。但是,我不想引用 YSerializationLibrary(它甚至可能不可用)。

目前,调用

typeof(SomeType).IsDefined(typeof(XSerializationOptions))

失败是因为 IsDefined 出于某种原因遍历所有属性并尝试解析它们的所有类型。异常看起来像:

System.IO.FileNotFoundException: Could not load file or assembly 'YSerializationLibrary, Version=1.2.3.4, Culture=neutral, PublicKeyToken=0123456789abcdef' or one of its dependencies. The system cannot find the file specified.
at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Int32 attributeCtorToken, Boolean mustBeInheritable)
at System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inherit)

有没有办法解决这个问题?如何在不引用完全不相关的 YSerializationLibrary 的情况下检查是否在 SomeType 上定义了 XSerializationOptions

一旦您考虑到 XSerializationLibrary 本身会调用 Enum.IsDefined,问题就会变得更糟;因此,除非您还引用 YSerializationLibrary,否则无法使用 SomeTypeXSerializationLibrary 进行序列化。

最佳答案

在您无法在运行时访问 Y 库的非常特殊的情况下,您可能会尝试使用您无权访问的具有相同名称和命名空间的新类来伪造该属性:

using System;
using System.Linq;
using ClassLibraryAssembly;
using OtherAssemblyX;

// This is a faking attribute, with same signature of that in unavailable assembly.
namespace OtherAssemblyY
{
public class YAttribute : Attribute
{
}
}

namespace MainAssembly
{
class Program
{
static void Main(string[] args)
{
var type = typeof (SomeType);

AppDomain.CurrentDomain.AssemblyResolve += (sender, eventArgs) =>
{
if (eventArgs.Name == "OtherAssemblyY, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
return typeof(Program).Assembly;
return null;
};

Console.WriteLine(type.IsDefined(typeof (XAttribute), true));

foreach (var attrObject in type.GetCustomAttributes(true))
{
Console.WriteLine("Attribute found: {0}, Assembly: {1}", attrObject, attrObject.GetType().Assembly);
}
}
}
}

结果:

True
Attribute found: OtherAssemblyY.YAttribute, Assembly: MainAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Attribute found: OtherAssemblyX.XAttribute, Assembly: OtherAssemblyX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

关于c# - 如何防止 MemberInfo.IsDefined 在不相关的属性上抛出 FileNotFoundException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25803956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com