2

我在通过 SmartAssembly 将包含我的 PostSharp 方面的程序集合并到我的项目中时遇到问题,我想知道是否有人可以提供帮助。

主要组件相当简单,看起来像:-

 class Program
{
    static void Main(string[] args)
    {
        var doer = new Doer();                

        doer.Do();

        Console.WriteLine("press any key to continue");
        Console.ReadKey();          
    }
}

[MethodDebugLogging(AttributeTargetElements = MulticastTargets.Method)]
public class Doer 
{
    public void Do()
    {
        Console.WriteLine("stuff and nonesense");
    }
}

MethodDebugLogging 方面工作正常,前提是它在同一个程序集中,但将其添加到自己的程序集中,然后通过 SmartAssembly 合并会导致:-

System.TypeInitializationException: The type initializer for '<>z__Aspects' thre
w an exception. ---> System.TypeInitializationException: The type initializer fo
r '<>z__AspectsImplementationDetails762586886' threw an exception. ---> System.I
O.FileNotFoundException: Could not load file or assembly 'Aspects, Version=1.0.0
.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system
 cannot find the file specified.
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String cod
eBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark&
stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppre
ssSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName as
semblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntr
ospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evid
ence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
   at System.Reflection.Assembly.Load(String assemblyString)
   at PostSharp.Aspects.Serialization.BinaryAspectSerializationBinder.BindToType
(String assemblyName, String typeName)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Bind(String as
semblyString, String typeString)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(Binary
AssemblyInfo assemblyInfo, String name)
   at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String obje
ctName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInf
ormationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, Bi
naryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWi
thMapTyped(BinaryObjectWithMapTyped record)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(He
aderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAp
pDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCr
ossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize
(Stream serializationStream, HeaderHandler handler, Boolean fCheck, IMethodCallM
essage methodCallMessage)
   at PostSharp.Aspects.Serialization.BinaryAspectSerializer.Deserialize(Stream
stream, IMetadataDispenser metadataDispenser)
   at PostSharp.Aspects.Serialization.AspectSerializer.Deserialize(Assembly asse
mbly, String resourceName, IMetadataDispenser metadataDispenser)
   at <>z__AspectsImplementationDetails762586886..cctor()
   --- End of inner exception stack trace ---
   at Obfuscation_Spike1.Doer.<>z__Aspects..cctor()
   --- End of inner exception stack trace ---
   at Obfuscation_Spike1.Doer.Do()
   at Obfuscation_Spike1.Program.Main(String[] )

方面本身相当简单:-

 [Serializable]
public class MethodDebugLogging :
     OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionArgs args)
    {
        Console.WriteLine("Entering: {0}.{1}", args.Method.DeclaringType, args.Method.Name);
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        Console.WriteLine("Exiting: {0}.{1}", args.Method.DeclaringType, args.Method.Name);
    }
}

在我看来,当我合并包含方面的程序集时,好像 Postsharp Distributable 的方面命名空间被剥离了,有没有人能够对此有所了解?

谢谢

保罗

4

1 回答 1

1

使您的程序集易于合并的最简单方法是使用[PSerializable]而不是[Serializable].

如果要继续使用[Serializable],则必须在运行时配置程序集绑定。有关详细信息,请参阅http://doc.postsharp.net/postsharp-3.0/##PostSharp-3.0.chm/html/fbd7975f-6c20-41d8-bda5-38392ed2ad00.htm

于 2013-09-06T12:05:18.107 回答