'Why am I getting a NullReferenceException when attempting to construct an XmlSerializer using a very simple Type with only string properties?

I've got a weird error here I don't quite understand. I have a very simple class with nothing but string properties and a couple of methods. One of those methods is a static function that returns a list of the object. When I attempt to instantiate an XmlSerializer with the type, though, I get an InvalidOperationException with a NullReferenceException inner exception . The declaration of properties and class looks like this:

[Serializable]
public class Config
{
    public string Name { get; set; }       
    public string DatabaseInstanceName { get; set; }
    public string InitialCatalog { get; set; }
    public string PersistSecurityInfo { get; set; } = "true";
    public string UserID { get; set; }
    public string Password { internal get; set; }

    public Config() { } //declared explicitly in case this was the pitfall, but didn't work
    public void Save()....
    public SqlConnection GetConn()...
    public static IList<Config> LoadAllConfigurations...

The static function where the error occurs doesn't get far.

  public static IList<Config> LoadAllConfigurations()
    {
        var t = typeof(Config);
        var xml = new XmlSerializer(t); //error occurs here

I confirmed that t contains the Type Config, so what about my instantiation am I doing incorrectly? As you can see, I added a parameterless constructor explicitly to see if there as a failure here, but nothing changed. Update: I also tried removing the default value for PersistSecurityInfo. InvalidOperationException outer exception says there was a problem reflecting type Config.



Solution 1:[1]

steve16351 is correct that it is the internal modifier on the get that causes this - the library code doesn't anticipate that scenario (which is pretty rare, to be fair).

Not in this case, but sometimes the trick with XmlSerializer is to unwrap all the exceptions, i.e.

    catch (Exception ex)
    {
        while (ex != null)
        {

            Console.WriteLine(ex.Message);
            ex = ex.InnerException;
        }
    }

However, in this case all it says is:

There was an error reflecting type 'Config'.
Object reference not set to an instance of an object.

However, sometimes this approach gives a more useful amount of information about the problem.

But: removing the internal fixes is. If you really really don't want that property to be gettable, then create two models - one that is your domain objects for regular usage, and one which is the serialization types just for use with the serializer. Then map between them adjacent to your serialization code. This approach is the "one stop shop" for fixing all nuances of serializers, since you can use whatever approaches the serializer likes, without impacting your "real" types at all.

Also: you can remove [Serializable] - XmlSerializer doesn't care about it.

Solution 2:[2]

Had the same error with a static property having a private getter like this:

public static string Test { private get; set; }

I could workaround the problem by removing the private. But in my case it made more sense to make XmlSerializer ignore the property like this:

[XmlIgnore]
public static string Test { private get; set; }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ZuBsPaCe