5

我正在尝试创建一些代码,可以将一个类的库序列化并反序列化为AutoCAD图形。这个问题与AutoCAD无关,除了它是我无法通过常规方式进行调试的原因之外。我从this article开始这个项目,并成功地让他的代码运行。然而,他的代码结构的方式,它需要我所有的类都继承自他的baseobject。由于这显然是代码味道,我知道我需要创建一个接口。以下是我结束的代码。序列化代码导致未处理的异常

第一部分是负责对AutoCAD图形进行序列化的代码。

第二部分是实现我的自定义序列接口

public class Commands 
{ 
    public class MyUtil 
    { 

     const int kMaxChunkSize = 127; 

     public ResultBuffer StreamToResBuf(MemoryStream ms, string appName) 
     { 

      ResultBuffer resBuf = new ResultBuffer(new TypedValue((int)DxfCode.ExtendedDataRegAppName, appName)); 

      for (int i = 0; i < ms.Length; i += kMaxChunkSize) 
      { 

       int length = (int)Math.Min(ms.Length - i, kMaxChunkSize); 

       byte[] datachunk = new byte[length]; 

       ms.Read(datachunk, 0, length); 

       resBuf.Add(new TypedValue((int)DxfCode.ExtendedDataBinaryChunk, datachunk)); 
      } 

      return resBuf; 
     } 

     public MemoryStream ResBufToStream(ResultBuffer resBuf) 
     { 

      MemoryStream ms = new MemoryStream(); 

      TypedValue[] values = resBuf.AsArray(); 

      // Start from 1 to skip application name 

      for (int i = 1; i < values.Length; i++) 
      { 

       byte[] datachunk = (byte[])values[i].Value; 

       ms.Write(datachunk, 0, datachunk.Length); 

      } 

      ms.Position = 0; 

      return ms; 

     } 

     public void NewFromEntity(IClearspanSerializable objectToSave, Entity ent) 
     { 

      using (ResultBuffer resBuf = ent.GetXDataForApplication("Member")) 
      { 
       BinaryFormatter bf = new BinaryFormatter(); 

       bf.Binder = new MyBinder(); 

       MemoryStream ms = this.ResBufToStream(resBuf); 
       objectToSave.SetObjectData(bf.Deserialize(ms)); 
      } 

     } 


     public void SaveToEntity(IClearspanSerializable objectToSave, Entity ent) 
     { 

      // Make sure application name is registered 
      // If we were to save the ResultBuffer to an Xrecord.Data, 
      // then we would not need to have a registered application name 

      Transaction tr = ent.Database.TransactionManager.TopTransaction; 

      RegAppTable regTable = (RegAppTable)tr.GetObject(ent.Database.RegAppTableId, OpenMode.ForWrite); 

      if (!regTable.Has("Member")) 
      { 
       RegAppTableRecord app = new RegAppTableRecord(); 

       app.Name = "Member"; 

       regTable.Add(app); 

       tr.AddNewlyCreatedDBObject(app, true); 
      } 


      BinaryFormatter bf = new BinaryFormatter(); 
      MemoryStream ms = new MemoryStream(); 

      bf.Serialize(ms, objectToSave); 
      ms.Position = 0; 

      ent.XData = this.StreamToResBuf(ms, "Member");; 


     } 
    } 

    public sealed class MyBinder : SerializationBinder 
    { 
     public override Type BindToType(string assemblyName, string typeName) 
     { 
      return Type.GetType(string.Format("{0}, {1}", 

       typeName, assemblyName)); 
     } 
    } 


    [CommandMethod("SaveClassToEntityXData", CommandFlags.Modal)] 
    public void SaveClassToEntityXData(IClearspanSerializable objectToSerialize) 
    { 

     Database db = Application.DocumentManager.MdiActiveDocument.Database; 
     Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 

     PromptEntityResult per = ed.GetEntity("Select entity to save class to:\n"); 

     if (per.Status != PromptStatus.OK) 
      return; 

     MyUtil util = new MyUtil(); 



     // Save it to the document 
     using (Transaction tr = db.TransactionManager.StartTransaction()) 
     { 

      Entity ent = (Entity)tr.GetObject(per.ObjectId, OpenMode.ForWrite); 

      util.SaveToEntity(objectToSerialize, ent); 

      tr.Commit(); 
     } 

     // Write some info about the results 

     //ed.WriteMessage("Content of MyClass we serialized:\n {0} \n", mc.ToString()); 

    } 


    [CommandMethod("GetClassFromEntityXData", CommandFlags.Modal)] 
    public void GetClassFromEntityXData(IClearspanSerializable objectToRestore) 
    { 

     Database db = Application.DocumentManager.MdiActiveDocument.Database; 
     Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 

     MyUtil util = new MyUtil(); 

     PromptEntityResult per = ed.GetEntity("Select entity to get class from:\n"); 

     if (per.Status != PromptStatus.OK) 
      return; 

     // Get back the class 
     using (Transaction tr = db.TransactionManager.StartTransaction()) 
     { 
      Entity ent = (Entity)tr.GetObject(per.ObjectId, OpenMode.ForRead); 
      util.NewFromEntity(objectToRestore, ent); 

      tr.Commit(); 

     } 

    } 
} 

这里是一个虚拟的类,我试图与测试类的例子:

[Serializable] 
public class MattMember: IClearspanSerializable 
{ 
    public string Name; 
    List<int> MattsInts; 


    public MattMember(string passedName, List<int> passedInts) 
    { 
     Name = passedName; 
     MattsInts = passedInts; 
    } 

    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] 
    public void GetObjectData(SerializationInfo info, StreamingContext context) 
    { 
     info.AddValue("Name", Name); 
     info.AddValue("MattsInts", MattsInts); 
    } 

    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] 
    public void SetObjectData(SerializationInfo info, StreamingContext context) 
    { 
     if (info == null) 
     { 
      throw new System.ArgumentNullException("info"); 
     } 
     Name = (string)info.GetValue("Name", typeof(string)); 

     MattsInts = (List<int>)info.GetValue("MattsInts", typeof(List<int>)); 
    } 


    void IClearspanSerializable.SetObjectData(object objectInDisguise) 
    { 


     if (objectInDisguise == null) 
     { 
      throw new System.ArgumentNullException("info"); 
     } 

     MattMember objectToCopy = (MattMember)objectInDisguise; 

     Name = objectToCopy.Name; 

     MattsInts = objectToCopy.MattsInts; 
    } 
} 

这里是接口:

public interface IClearspanSerializable 
{ 
    void GetObjectData(SerializationInfo info, StreamingContext context); 
    void SetObjectData(object objectInDisguise); 
} 

当我尝试运行AutoCAD内部的代码时,我所得到的就是这个错误要么。这导致我相信在我的类的初始化中存在一个简单的错误。非我的断点受到打击。

Unhandled Exception

我应该如何调试呢?我在哪里搞砸了我的初始化?

[编辑],尤其是圆形是什么,是在 “详细信息”:

****************************************************************************** 
Application does not support just-in-time (JIT) 
debugging. See the end of this message for details. 

************** Exception Text ************** 
System.ArgumentException: Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type. 
    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure) 
    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method) 
    at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction) 
    at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorkerWithExceptionFilter(MethodInfo mi, Object commandObject, Boolean bLispFunction) 
    at Autodesk.AutoCAD.Runtime.PerDocumentCommandClass.Invoke(MethodInfo mi, Boolean bLispFunction) 
    at Autodesk.AutoCAD.Runtime.CommandClass.CommandThunk.Invoke() 


************** Loaded Assemblies ************** 
mscorlib 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18444 built by: FX451RTMGDR 
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll 
---------------------------------------- 
Acdbmgd 
    Assembly Version: 20.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/AcdbMgd.DLL 
---------------------------------------- 
adui20 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/adui20.DLL 
---------------------------------------- 
AdUiPalettes 
    Assembly Version: 20.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/AdUiPalettes.DLL 
---------------------------------------- 
WindowsBase 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/WindowsBase/v4.0_4.0.0.0__31bf3856ad364e35/WindowsBase.dll 
---------------------------------------- 
System 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll 
---------------------------------------- 
PresentationFramework 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.dll 
---------------------------------------- 
PresentationCore 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/PresentationCore/v4.0_4.0.0.0__31bf3856ad364e35/PresentationCore.dll 
---------------------------------------- 
System.Xaml 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xaml/v4.0_4.0.0.0__b77a5c561934e089/System.Xaml.dll 
---------------------------------------- 
System.Configuration 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll 
---------------------------------------- 
System.Xml 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll 
---------------------------------------- 
AdApplicationFrame 
    Assembly Version: 0.0.0.0 
    Win32 Version: 5.2.8.100 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/AdApplicationFrame.DLL 
---------------------------------------- 
AdWindows 
    Assembly Version: 5.2.10.200 
    Win32 Version: 5.2.10.200 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/AdWindows.DLL 
---------------------------------------- 
PresentationFramework.Classic 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework.classic/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.classic.dll 
---------------------------------------- 
System.Drawing 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll 
---------------------------------------- 
accoremgd 
    Assembly Version: 20.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/accoremgd.DLL 
---------------------------------------- 
System.Core 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll 
---------------------------------------- 
Acmgd 
    Assembly Version: 20.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/Acmgd.DLL 
---------------------------------------- 
AcWindows 
    Assembly Version: 20.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/AcWindows.DLL 
---------------------------------------- 
AcWindows.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcWindows.resources.DLL 
---------------------------------------- 
AcCui 
    Assembly Version: 20.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/AcCui.DLL 
---------------------------------------- 
PresentationFramework-SystemXml 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemXml/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemXml.dll 
---------------------------------------- 
PresentationFramework.Aero 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework.Aero/v4.0_4.0.0.0__31bf3856ad364e35/PresentationFramework.Aero.dll 
---------------------------------------- 
WindowsFormsIntegration 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/WindowsFormsIntegration/v4.0_4.0.0.0__31bf3856ad364e35/WindowsFormsIntegration.dll 
---------------------------------------- 
System.Windows.Forms 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll 
---------------------------------------- 
PresentationUI 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationUI/v4.0_4.0.0.0__31bf3856ad364e35/PresentationUI.dll 
---------------------------------------- 
System.Xml.Linq 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml.Linq/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.Linq.dll 
---------------------------------------- 
PresentationFramework-SystemXmlLinq 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemXmlLinq/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemXmlLinq.dll 
---------------------------------------- 
FeaturedAppsPlugin 
    Assembly Version: 20.0.0.0 
    Win32 Version: 20.0.46.0.0 
    CodeBase: file:///C:/ProgramData/Autodesk/ApplicationPlugins/Autodesk%20FeaturedApps.bundle/Contents/Windows/2015/Win32/FeaturedAppsPlugin.dll 
---------------------------------------- 
UIAutomationTypes 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 built by: FX451RTMGREL 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationTypes/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationTypes.dll 
---------------------------------------- 
PresentationFramework-SystemCore 
    Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.18408 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/PresentationFramework-SystemCore/v4.0_4.0.0.0__b77a5c561934e089/PresentationFramework-SystemCore.dll 
---------------------------------------- 
Anonymously Hosted DynamicMethods Assembly 
    Assembly Version: 0.0.0.0 
    Win32 Version: 4.0.30319.18444 built by: FX451RTMGDR 
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll 
---------------------------------------- 
AcLayer 
    Assembly Version: 20.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/AcLayer.DLL 
---------------------------------------- 
AcLayer.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcLayer.resources.DLL 
---------------------------------------- 
AcAeNet.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcAeNet.resources.DLL 
---------------------------------------- 
AcCloudRender.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcCloudRender.resources.DLL 
---------------------------------------- 
AcCustomize.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcCustomize.resources.DLL 
---------------------------------------- 
AcDxWizard.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcDxWizard.resources.DLL 
---------------------------------------- 
AcExportLayoutUI.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcExportLayoutUI.resources.DLL 
---------------------------------------- 
AcInterfere.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcInterfere.resources.DLL 
---------------------------------------- 
AcLayerTools.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcLayerTools.resources.DLL 
---------------------------------------- 
AcMrUi.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcMrUi.resources.DLL 
---------------------------------------- 
AcMultiLineUi.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcMultiLineUi.resources.DLL 
---------------------------------------- 
AcRecoverAll.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcRecoverAll.resources.DLL 
---------------------------------------- 
AcScaleList.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcScaleList.resources.DLL 
---------------------------------------- 
AcUnderlay.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcUnderlay.resources.DLL 
---------------------------------------- 
AcViewTransitionsUi.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcViewTransitionsUi.resources.DLL 
---------------------------------------- 
AdskConnectionPointMgd.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AdskConnectionPointMgd.resources.DLL 
---------------------------------------- 
AcCalcUi.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcCalcUi.resources.DLL 
---------------------------------------- 
AcLivePreviewContext 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/AcWindows.dll 
---------------------------------------- 
AcDialogToolTips 
    Assembly Version: 20.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/AcDialogToolTips.DLL 
---------------------------------------- 
AcDialogToolTips.resources 
    Assembly Version: 0.0.0.0 
    Win32 Version: 20.0.51.0.0 
    CodeBase: file:///C:/Program%20Files/Autodesk/AutoCAD%202015/en-US/AcDialogToolTips.resources.DLL 
---------------------------------------- 
Write To Block 
    Assembly Version: 1.0.5276.26438 
    Win32 Version: 1.0.0.0 
    CodeBase: file:///C:/Users/Administrator/Documents/Clearspan/AutoCAD%20Projects/Write%20To%20Block/Write%20To%20Block/bin/Debug/Write%20To%20Block.dll 
---------------------------------------- 

************** JIT Debugging ************** 
Application does not support Windows Forms just-in-time (JIT) 
debugging. Contact the application author for more 
information. 
+0

请先单击详细信息按钮并粘贴细节后。我不知道为什么这个问题得到5 upvotes ..... – Matt

+0

什么版本/ bitness的AutoCAD是这样的? –

+0

这是32位Windows 7机器上的AutoCAD 2015 –

回答

1

System.ArgumentException:因为它 签名或安全透明性与该 委托类型兼容无法绑定到目标方法。

我的理解是,你的接口的“GetObjectData”方法与ISerializable方法冲突,在BinnaryFormatter厨房的某处。

只要重构你MyUtil的所有转换功能(StreamToResBuf,ResBufToStream ...),你应该能够序列化/反序列化标有[Serializable]属性的任何类
你的哑类应该是这样的(收集和原始类型的自动序列化,只在需要时实现了ISerializable):

[Serializable] 
public class MattMember 
{ 
    public string Name; 
    List<int> MattsInts; 
} 

的NewFromEntity方法(避免这里的任何不必要的施法):

public object NewFromEntity(Entity ent) 
     {  
      using (ResultBuffer resBuf = ent.GetXDataForApplication("Member")) 
      { 
       BinaryFormatter bf = new BinaryFormatter(); 

       bf.Binder = new MyBinder(); 

       MemoryStream ms = this.ResBufToStream(resBuf); 
       return bf.Deserialize(ms); 
      }  
     } 

最后是你的命令:

[CommandMethod("GetClassFromEntityXData", CommandFlags.Modal)] 
//Updated return type here but don't know if it is correct with AutoCAD 
public object GetClassFromEntityXData() 
{ 
    Database db = Application.DocumentManager.MdiActiveDocument.Database; 
    Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; 
    object objectToRestore; 

    MyUtil util = new MyUtil(); 

    PromptEntityResult per = ed.GetEntity("Select entity to get class from:\n"); 

    if (per.Status != PromptStatus.OK) 
     return; 

    // Get back the class 
    using (Transaction tr = db.TransactionManager.StartTransaction()) 
    { 
     Entity ent = (Entity)tr.GetObject(per.ObjectId, OpenMode.ForRead); 
     //Cast to your IClearspan interface here, or use Reflection 
     // to determine deserialized object's Type 
     objectToRestore = util.NewFromEntity(ent); 

     tr.Commit(); 
    } 

    return objectToRestore; 
} 
0

瞎猜:你需要有一个默认的构造函数,因为别人constucts你的对象:

public MattMember() 
{ 

} 

或者像这样的特殊的非结构化构造器:

protected MattMember(SerializationInfo info, StreamingContext context) 
    { 
// Set object data  
    } 

也许你的界面也需要继承自ISerializable

+0

谢谢,但我也尝试过所有这些 –

1
at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure) 
    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method) 
    at Autodesk.AutoCAD.Runtime.CommandClass.InvokeWorker(MethodInfo mi, Object commandObject, Boolean bLispFunction) 

正确解释堆栈跟踪非常重要。您发布的代码都不涉及,它从未开始。这种失败时,AutoCAD试图打电话给你的命令处理程序:

[CommandMethod("GetClassFromEntityXData", CommandFlags.Modal)] 
public void GetClassFromEntityXData(IClearspanSerializable objectToRestore) 
// etc.. 

AutoCAD是不会给你objectToRestore的说法,它不知道你的接口豆。这就是例外的意思,它不能将委托绑定到具有神秘参数的方法。请注意,您开始的示例代码如何使用不带参数的方法。以及它如何使用Editor.GetEntity()来允许用户选择一个绘图实体。我假设你需要一个类似的方案,我不太了解AutoCAD脚本。

0
[CommandMethod("SaveClassToEntityXData", CommandFlags.Modal)] 
public void SaveClassToEntityXData(IClearspanSerializable objectToSerialize) 

实现该命令的函数不能接受参数,但在命令实现中,您可以使用命令行输入函数(如可在Editor类下找到的GetString(),GetInteger()等)接受参数。

http://adndevblog.typepad.com/autocad/2012/07/create-command-with-parameters.html

相关问题