0

Estoy desarrollando una aplicación donde debo serializar una clase en un Json, en esta clase existe una propiedad que es un arreglo y el tipo es object, fíjense este es la clase donde se encuentra el atributo itemsField y la propiedad Items.

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.w3.org/2000/09/xmldsig#")]
[System.Xml.Serialization.XmlRootAttribute("Transform", Namespace="http://www.w3.org/2000/09/xmldsig#", IsNullable=false)]
public partial class TransformType {

    private object[] itemsField;

    private string[] textField;

    private string algorithmField;

    /// <comentarios/>
    [System.Xml.Serialization.XmlAnyElementAttribute()]
    [System.Xml.Serialization.XmlElementAttribute("XPath", typeof(string))]
    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }

    /// <comentarios/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string[] Text {
        get {
            return this.textField;
        }
        set {
            this.textField = value;
        }
    }

    /// <comentarios/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="anyURI")]
    public string Algorithm {
        get {
            return this.algorithmField;
        }
        set {
            this.algorithmField = value;
        }
    }
}

luego escribo esto para asignar un valor pero me dice ERROR NO SE PUEDE INICIALIZAR EL TIPO OBJECT, ITEMS NO EXISTE EN EL CONTEXTO ACTUAL... ¿Que estoy haciendo mal?

 Items = new object[1]
      {
       new object
            {
              Items[1]="",
            }
      }
0

3 respuestas 3

2

Si quieres inicializar dicha propiedad con un elemento del tipo cadena podrías hacer:

TransformType transformType = new TransformType();  
transformType.Items = new[] { "" };

DEMO

1

Tendras que hacer referencia al namespace y clase de donde tengas tu object con using, para ello primero incluye tu object dentro de un namespace para apuntar a el:

namespace MyObject
{
    private object[] itemsField;

    public object[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }

}

y en tu script añades con using tu namespace:

using MyObject;

o bien lo llamas directamente desde tu declaración de la siguiente manera:

 Items = new MyObject.object[1]
  {
   new object
        {
          Items[1]="",
        }
  }
1
  • Hola JorelDraw...Edite mi pregunta para que puedas observar si tu respuesta no afecta otros aspecto del codigo el 11 sep. 2017 a las 12:51
0

De esta manera funciono

Transforms = new TransformType [1]
       {
         new TransformType
             {
             Items = new string[1] {Valor a Asignar},
             Text = new string [1]  { Valor a Asignar },
             Algorithm=Valor a Asignar                                    
              }
       }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.