Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net XML序列化委托_.net_Vb.net_.net 2.0_Xml Serialization - Fatal编程技术网

.net XML序列化委托

.net XML序列化委托,.net,vb.net,.net-2.0,xml-serialization,.net,Vb.net,.net 2.0,Xml Serialization,我有一个想要序列化的类,但包含一个显然无法序列化的委托的公共实例: <Serializable()> Class A Public Delegate Function TestEventHandler(ByVal myObj as CutomObject, _ ByVal myObj2 as CustomObject) as Boolean ' does not 'want' to b

我有一个想要序列化的类,但包含一个显然无法序列化的委托的公共实例:

<Serializable()> Class A
    Public Delegate Function TestEventHandler(ByVal myObj as CutomObject, _
                                         ByVal myObj2 as CustomObject) as Boolean

    ' does not 'want' to be serialized - cause: no parameterless constructor'
    Public TestDelegate as TestEventHandler
End Class
A类
公共委托函数TestEventHandler(由Val MyObject作为CutomObject_
ByVal myObj2作为自定义对象)作为布尔值
'不'希望'序列化-原因:没有无参数构造函数'
作为TestEventHandler的公共TestDelegate
末级
我使用了
,这很有帮助-我的意思是这个例外不再针对这个成员


但是,有没有办法使其可序列化?

您的问题是

委托不能序列化是可以理解的,因为它可以被视为“函数指针”,而不是实际数据

您可以通过对该代理应用
非序列化
属性来解决问题

[Serializable]
public class Test
{
    [NonSerialized]
    public TestEventHandler TestDelegate;
}
但是,请记住,Xml序列化需要其他属性,然后通过
BinaryFormatter
SoapFormatter
类进行“常规”序列化

使用XmlSerialization时,应该使用
XmlIgnore
属性。 使用XmlSerialization时,不必将类标记为可序列化的
。
相反,它应该有一个默认的公共构造函数。XmlSerialization仅序列化具有getter和setter的公共属性。

您可以通过XmlSerialization来控制对象的序列化方式。

您的问题是

委托不能序列化是可以理解的,因为它可以被视为“函数指针”,而不是实际数据

您可以通过对该代理应用
非序列化
属性来解决问题

[Serializable]
public class Test
{
    [NonSerialized]
    public TestEventHandler TestDelegate;
}
但是,请记住,Xml序列化需要其他属性,然后通过
BinaryFormatter
SoapFormatter
类进行“常规”序列化

使用XmlSerialization时,应该使用
XmlIgnore
属性。 使用XmlSerialization时,不必将类标记为可序列化的
。
相反,它应该有一个默认的公共构造函数。XmlSerialization仅序列化具有getter和setter的公共属性。

您可以通过控制对象通过XmlSerialization进行序列化的方式。

在委托上使用
[XmlIgnore]

[Serializable]
public class Test
{
    [NonSerialized]
    public TestEventHandler TestDelegate;
}


不,无法序列化委托。它会是什么样子?除了.NET以外的平台会用它做什么?

在代理上使用
[XmlIgnore]

[Serializable]
public class Test
{
    [NonSerialized]
    public TestEventHandler TestDelegate;
}


不,无法序列化委托。它看起来会是什么样子?除了.NET以外的平台会用它做什么?

您可以尝试在4.0中使用Func委托。它们是可序列化的。

您可以尝试在4.0中使用Func委托。它们是可序列化的。

您还必须将要调用的委托或方法的名称作为字符串存储在类中

我之所以使用这个函数,是因为我想配置在配置文件中通过ftp下载文件后调用什么函数,即不同的ftp下载配置通过调用配置的方法(委托)以不同的方式处理下载的文件

然后,当您想要使用委托时,在本例中,在我下载文件之后。我使用存储在序列化配置文件中的字符串创建委托

 //Create the delegate method if it has been set.
 if (!String.IsNullOrEmpty(ftpReceive.ProcessDownloadFileMethod) && ftpReceive.ProcessFile == null)
 {
 //Create the delegate.
 Type t = typeof(FTPTransfer);
 ftpReceive.ProcessFile = (FTPTransfer.ProcessDownloadedFile) Delegate.CreateDelegate(typeof(FTPTransfer.ProcessDownloadedFile), t.GetMethod(ftpReceive.ProcessDownloadFileMethod));
 }
因此,理想情况下,您希望将要调用的函数/委托的名称存储为字符串,然后在运行时使用反射从字符串创建它

希望这有帮助

或者,如果要调用的方法只存在于一个类中,则可以通过包装属性来简化它

 //HACK: Cannot serialise delegates.
 public string ProcessDownloadFileMethod { get; set; }

 //HACK: Cannot serialise delegates.
 private ProcessDownloadedFile _processFile;
 [XmlIgnore]
 public ProcessDownloadedFile ProcessFile
 {
   get
   {
     if (_processFile == null && !String.IsNullOrEmpty(ProcessDownloadFileMethod))
     {
        Type t = this.GetType();
        this._processFile = (ProcessDownloadedFile) Delegate.CreateDelegate(typeof(ProcessDownloadedFile), t.GetMethod(this.ProcessDownloadFileMethod));

      }
      return _processFile;
    }
    set
      {
         if (value != null)
         {
            ProcessDownloadFileMethod = value.Method.Name;
         } else {
            ProcessDownloadFileMethod = null;
         }
         _processFile = value;
       }
    }

要真正使其可配置,最好包括用于创建委托的类/类型以及函数/方法名称。

您还必须将要调用的委托或方法的名称作为字符串存储在类中

[Serializable]
public class Test
{
    [NonSerialized]
    public TestEventHandler TestDelegate;
}
我之所以使用这个函数,是因为我想配置在配置文件中通过ftp下载文件后调用什么函数,即不同的ftp下载配置通过调用配置的方法(委托)以不同的方式处理下载的文件

然后,当您想要使用委托时,在本例中,在我下载文件之后。我使用存储在序列化配置文件中的字符串创建委托

 //Create the delegate method if it has been set.
 if (!String.IsNullOrEmpty(ftpReceive.ProcessDownloadFileMethod) && ftpReceive.ProcessFile == null)
 {
 //Create the delegate.
 Type t = typeof(FTPTransfer);
 ftpReceive.ProcessFile = (FTPTransfer.ProcessDownloadedFile) Delegate.CreateDelegate(typeof(FTPTransfer.ProcessDownloadedFile), t.GetMethod(ftpReceive.ProcessDownloadFileMethod));
 }
因此,理想情况下,您希望将要调用的函数/委托的名称存储为字符串,然后在运行时使用反射从字符串创建它

希望这有帮助

或者,如果要调用的方法只存在于一个类中,则可以通过包装属性来简化它

 //HACK: Cannot serialise delegates.
 public string ProcessDownloadFileMethod { get; set; }

 //HACK: Cannot serialise delegates.
 private ProcessDownloadedFile _processFile;
 [XmlIgnore]
 public ProcessDownloadedFile ProcessFile
 {
   get
   {
     if (_processFile == null && !String.IsNullOrEmpty(ProcessDownloadFileMethod))
     {
        Type t = this.GetType();
        this._processFile = (ProcessDownloadedFile) Delegate.CreateDelegate(typeof(ProcessDownloadedFile), t.GetMethod(this.ProcessDownloadFileMethod));

      }
      return _processFile;
    }
    set
      {
         if (value != null)
         {
            ProcessDownloadFileMethod = value.Method.Name;
         } else {
            ProcessDownloadFileMethod = null;
         }
         _processFile = value;
       }
    }

要真正实现这种可配置性,最好包括用于创建委托的类/类型以及函数/方法名。

我使用XmlSerialization,因此非序列化没有帮助。我过去用过,但它有用。现在,是否有可能序列化委托?@Frederik:
[NonSerialized]
不适用于XML序列化。这就是我要说的:XmlSerialization和常规序列化之间有区别。从Topicstarts openingpost上看,当我回答这个问题时,并不清楚他使用了哪种序列化。我使用XmlSerialization,所以非序列化没有帮助。我过去用过,但它有用。现在,是否有可能序列化委托?@Frederik:
[NonSerialized]
不适用于XML序列化。这就是我要说的:XmlSerialization和常规序列化之间有区别。从Topicstarts openingpost来看,当我回答这个问题时,并不清楚他使用了哪种序列化。
[Serializable]
public class Test
{
    [NonSerialized]
    public TestEventHandler TestDelegate;
}