开发者

Why doesn't this struct serialize?

开发者 https://www.devze.com 2023-01-20 17:08 出处:网络
I have the following struct: public struct Declarations { public const string SchemaVersion = \"urn:ean.ucc:gdsn:2\";

I have the following struct:

public struct Declarations
{
 public const string SchemaVersion = "urn:ean.ucc:gdsn:2";
}

And the SchemaVersion is used by some XmlElements which are trying to be serialized like this:

[XmlElement(Type=typeof(SomeGarbage),ElementName="moreJunk",IsNullable=false,Form=XmlSchemaForm.Unqualified,Namespace=Declarations.SchemaVersion)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public SomeType __someMember;

I'm getting the following error when I try to run sgen.exe on my dll:

Error 1开发者_JAVA百科 There was an error reflecting type 'BigHammer.CINParser.Messages.Declarations'

Error 2 The command ""C:\Program Files (x86)\Microsoft Visual Studio 8\SDK\v2.0\Bin\sgen.exe" /force "C:\Users\mstoddard\teamserver_source\GDSN\CINParser\CINParser\bin\Debug\BigHammer.CINParser.dll" " exited with code 1.

I tried making the struct a class, no help

I tried adding a default constructor, no help

Thoughts?

===Update===

It looks like Visual Studio was hiding some information, I have The same classes defined in two different CLR namespaces but not XML namespaces:

Error: There was an error reflecting type 'BigHammer.CINParser.Messages.Declarations'. - Types 'BigHammer.CINParser.Messages.Declarations' and 'BigHammer.CINParser.Messages.urn_ean_ucc_2.Declarations' both use the XML type name, 'Declarations',from namespace ''. Use XML attributes to specify a unique XML name and/or namespace for the type.


Serialisation only applies to instance data.

Static/Const are data tied to the class.

Eg In the given app domain Declarations.SchemaVersion can only be a single value. So there is no instance data to serialise.


Xml serializer serializes only properties.

Wrap your variables.

class Cro
{
     public Cro
     {
          _atia="my const value";
     }
     private string _atia; // won't be serialized
     public string Atia //  will be serialized
     {
          get
          {
               return _atia;
          }
     }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号