C# SQL CLR Project - Public Generic.List items show up as non-public members in debugger

StackOverflow https://stackoverflow.com/questions/21391680

  •  03-10-2022
  •  | 
  •  

Question

I'm having an issue in a C# SQL CLR project where publicly declared Generic.List items are not showing up as public in the debugger. This is problematic because I'm trying to serialize the object to XML. The real project has some complex objects, but a very simple example would be something like this:

[Microsoft.SqlServer.Server.SqlProcedure]
public static void DoSomething()
{

    // the Thing Object has a public property of List<string> 
    // that is created in the constructor
    Thing t = new Thing();

    t.Items.Add("test");
    t.Items.Add("test2");

    // now, if I look at t in the debugger, it has given the
    // items list a capacity of 4 and shows the items as
    // non-public members (2 with values and 2 that are null)

}

I wanted to post an image from the debugger here, but it appears I cannot do that as a new user...

With all of that said, is this behavior because it is a SQL CLR project? The same code worked fine when developing in a console application (though I guess I could have messed something up when adding it to the CLR project).

Adding additional detail on the Thing object - it includes the following declaration for the Items:

public List<string> Items { get; set; }

Then, in the debugger, the Items just shows Capacity and Count (no actual items or properties for the Items). The actual item instances are listed under Non-Public members.

Was it helpful?

Solution

The Items property itself can never be seen by a debugger, because it is an indexed property, so there isn't a value you can just get for it, and hence examining the value it returns doesn't make sense (like examining the 30th of February or a politician's kept promises; it just doesn't exist).

List<T> uses the DebuggerTypeProxyAttribute attribute to define another class to be used to give a debugger eye view. This class (one internal to mscorlib) has a public Items property that returns an array with a copy of the list's items, so that it looks like you can actually examine the Items property of List<T> when really you are examining the Items property of an object that copies out the items when invoked.

Debuggers don't have to use this approach, so maybe the one you are using doesn't, or there's some other restrictions, but if you are using a debugger that doesn't support DebuggerTypeProxy you can just examine the private member that stores the items in an array (the array will be at least as large as Count, and possibly a bit larger to leave growing room rather than resizing on each Add; you can ignore elements beyond Count - 1).

This has nothing to do with XML serialisation, so whatever problem you are having with that is likely completely unrelated.

Edit:

I see some people are having other problems with XML serialisation and the SQL CLR as per these:

SQL Server not finding serialization assembly

http://connect.microsoft.com/VisualStudio/feedback/details/753005/when-deploying-a-sqlclr-assembly-with-a-generated-xmlserializer-assembly-the-xmlserializer-assembly-should-be-deployed

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/3fa5dce3-b0f3-44f8-9b7b-65439f1c98ae/cannot-deploy-xmlserializers-clr-assemblies?forum=ssdt

http://social.msdn.microsoft.com/Forums/sqlserver/en-US/e6560fa4-76f1-4da2-b795-7926d0743baa/sql-clr-problem-with-xmlserializer?forum=sqlnetfx

Your XML problem is indeed different to your debugger view issue. (I meanwhile am off to add debugger proxies to some of my collection types, I've always figured people would just skip through to the inner members and not care, but you've made me rethink that one).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top