C# - (this T obj) - feature name

Markus Freitag 3,786 Reputation points
2020-12-21T10:42:17.71+00:00

Hello,

I have a function like this. Works well, but why?

public static string ToXML<T>(this T obj)

I call it without parameter! I think the reason is this
What is this mechanism called?
How can I see if this exists, perhaps via IntelliSense?

var xmlBody = message.ToXML();
Console.WriteLine("xml:" + xmlBody);

AtnSWO

using System;
using System.Xml.Serialization;
using System.Collections.Generic;

using myTest;

namespace myTest
{
    public class Program
    {
        public static void Main()
        {
            var invoice = new Invoice(Guid.NewGuid(), 123) { Name = "Jürgen Bauer"};
            var body = new Body();
            body.AddInvoice(invoice);

            var message = new Message() { Body = body };

            var xmlBody = message.ToXML();
            Console.WriteLine("xml:" + xmlBody);
        }
    }

    [XmlType("invoice")]
    public class Invoice
    {
        public Invoice()
        {
            this.EventId = Guid.NewGuid();
        }

        public Invoice(Guid eventId, System.Int64 number)
        {
            this.EventId = eventId;
            this.Number = number;
        }

        [XmlAttribute("eventid")]
        public System.Guid EventId
        {
            get;
            set;
        }

        [XmlAttribute("number")]
        public System.Int64 Number
        {
            get;
            set;
        }

        [XmlElement("name")]
        public string Name
        {
            get;
            set;
        }

        // Other Code //
    }

    public static class XMLHelper
    {
        public static string ToXML<T>(this T obj)
        {
            using (var stringwriter = new System.IO.StringWriter())
            {
                var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                serializer.Serialize(stringwriter, obj);
                return stringwriter.ToString();
            }
        }
    }

    [XmlType("message")]
    public class Message
    {
        [XmlElement("body")]
        public Body Body {get; set;}
    }


    public class Body
    {
        public Body()
        {
            this.SaveInvoices = new List<Invoice>();
        }
        [XmlArray("saveInvoice")]
        public List<Invoice> SaveInvoices {get; private set;}

        public void AddInvoice (Invoice invoice)
        {
            this.SaveInvoices.Add(invoice);
        }
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,908 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,421 Reputation points
    2020-12-21T18:42:58.887+00:00

    Hi @Markus Freitag

    Which class are you referring too, a specific third party assembly? Two options and may depend on if a .pdb file exists, one using Visual Studio, one a paid for tool (there may be similar tools to look at, I favor dopeek).

    Both ideas use the same .DLL files. Object browser provides signatures while dotpeek (paid for tool) provides decompiling.

    Visual Studio Object browser

    50029-objectbrowser.png

    Jet brains dotpeek

    50104-jetpeek.png

    Another thought is to set

    Tools -> Options -> Debugging -> Uncheck "Just my Code"

    Then place your mouse cursor on something and right click to goto definition (I'm guessing as I have a extension which overrides goto definition).

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Cheong00 3,476 Reputation points
    2020-12-21T10:54:05.17+00:00

    1) This is called Extension Methods.

    2) You have to compile it once first at least for older version of Visual Studios.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.