Customizing Dump in a DataContext package

Hi, I'm having an issue with the ToDump function inside a DataContext package. I'm creating a static method as per documentation but it doesn't seem to be called and the output does not get formatted the way I want it to.

Example:

public sealed class MongoDriver : DynamicDataContextDriver
{
        public static object ToDump(object input)
        {
            var document = input as BsonDocument;
            return document != null ? BsonTypeMapper.MapToDotNetValue(document) : input;
        }

        // unrelated code here
}

Is there something I'm missing or is this a limitation of the DataContext?

Comments

  • i2van
    edited March 2025

    static object ToDump(object input) should be in Program context and not in class, (example).

    It should be member method in your case, (example).

  • JoeAlbahari
    edited March 2025

    The static ToDump method applies to scripts and My Extensions - not drivers.

    With drivers, you instead override the PreprocessObjectToWrite method. This is described in the documentation:
    https://www.linqpad.net/Extensibility.aspx

  • @JoeAlbahari said:
    The static ToDump method applies to scripts and My Extensions - not drivers.

    With drivers, you instead override the PreprocessObjectToWrite method. This is described in the documentation:
    https://www.linqpad.net/Extensibility.aspx

    Oh, I see. Thank you, Joseph!

    For those that might come after, I've achieved what I wanted with just a small modification of my code:

    public sealed class MongoDriver : DynamicDataContextDriver
    {
            public override void PreprocessObjectToWrite(ref object objectToWrite, ObjectGraphInfo info)
            {
                var document = objectToWrite as BsonDocument;
                if (document != null)
                {
                    objectToWrite = BsonTypeMapper.MapToDotNetValue(document);
                    return;
                }
            }
    
            // unrelated code here
    }