Asignación de tipo C # y llamada de método en una sola declaración

Tengo esto:

var doc = new XmlDocument();
doc.LoadXml("<XMLHERE/>"); //This returns void

How do I combine into one statement?

preguntado el 31 de enero de 12 a las 08:01

new Action(() => { var doc = new XmlDocument(); doc.LoadXml("XML"); }).Invoke(); Almost single statement )) -

Otra cosa new Action<XmlDocument>((x) => x.Load("Your xml"))(new XmlDocument()); -

6 Respuestas

Use static XDocument.Parse(string xml) which returns an XDocument object;

    XDocument doc = XDocument.Parse("<XMLHERE/>");

Respondido el 31 de enero de 12 a las 12:01

Good answer, if the user has the supporting .NET version (3.5 and above). - Grant Thomas

var foo = XDocument.Load(new MemoryStream(Encoding.UTF8.GetBytes("<xml />"))); The overload of static member of XDocument Load that accepts string as a parameter expects URI - Oybek

This is great but has to be from System.Xml unfortunately. - tom redfern

You can't, lest you lose the reference to the document (doc) and therefore find it to be inaccessible.

As other answers have began filtering in to contradict my assertion, what I will say is this. Of course, usted puede create a method to do 'the dirty work' for you, but then you're not really just turning this into a one-liner, you're just disjointing the creation (and not really saving unless you need to write this in hundreds of different places).

This might not be a bad thing in many situations, but defining a class with a single extension method to facilitate this seems ridiculous. Specifying it locally, within a class where this would be extensively utilised could be a different matter.

All in all, my answer still stands fundamentally, not considering the many contrived ways you might 'get around' this.

Respondido el 31 de enero de 12 a las 12:01

Thanks - as I suspected. Marked this as the answer but thanks for everyone's responses. Much appreciated. - tom redfern

this is probably your best (alternative to two lines) opción...

public static XmlDocument MyLazyAssFunction(xml)
{
    var doc = new XmlDocument();
    doc.LoadXml(xml);
    return doc;
}

then here is your single statment...

var doc = MyLazyAssFunction("<XMLHERE/>");

The point here being that your original two lines is a perfectly fine way of doing what you need to do.. and it is very readable as it stands too

Respondido el 31 de enero de 12 a las 14:01

LOL! Good name for the function, I must say! :D By the way, you forgot estático. - Sergey Vyacheslavovich Brunov

I think the 'best' option would be to write two lines, rather than create a class for this sole purpose. - Grant Thomas

This will also cause a compile-time error: you must return doc. - Grant Thomas

@Mr.Disappointment: Not sure why I missed that out. Am I that unknowingly reliant on VS prompts :S - musefan

Con solo el XmlDocument API you can't do this in a single line and keep the XmlDocument reference. However you could write a helper API

public static class XmlUtils {
  public static XmlDocument CreateDocument(string xml) {
    var doc = new XmlDocument();
    doc.LoadXml(xml);
    return doc;
  }
}

var doc = XmlUtils.CreateDocument("<XMLHERE/>");

Respondido el 31 de enero de 12 a las 12:01

You cannot. If defining the variable does not count as a statement, you can write:

XmlDocument doc;
(doc = new XmlDocument()).LoadXml("<XMLHERE/>");

Respondido el 31 de enero de 12 a las 12:01

Puedes escribir extension:

public static XmlDocument MyLoad(this XmlDocument doc, string xml)
{
    doc.LoadXml(xml);
    return doc;
}

Uso:

var doc = new XmlDocument().MyLoad(xml); 

Respondido el 31 de enero de 12 a las 12:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.