0

I am c# silvelight5 beginner and under a situation that i have to create my own Converter() function which must be similar like Converter() function derived from IValueConverter Interface.

What data i have right now ? I have correctly deserialized xml and on deserializing i got this:

XmlSerializer deserializer = new XmlSerializer(typeof(Parameter));//this Parameter is class obtained from(Root node of xml) and "parameter" is object.
XmlReader reader = XmlReader.Create(new StringReader(xmlstring));
Parameter parameter = (Parameter)deserializer.Deserialize(reader);
foreach (var item in parameter.Component.Attributes.Items)
{
    Debug.WriteLine(item);//It works correctly i have debugged
}

As you can see that i have object parameter which now contains the xml elements and roots (I mean all the data). Using this object now i tried to create Converter equivalent like this(his code is also in the same class):

ICollection<Parameter> list = parameter as ICollection<Parameter>;// **Problem creating line**
List<UIElement> result = new List<UIElement>();   

You can see this "parameter" is obtained from deserialization and i was sure on debugging that it is deserialized properly. So here i have replaced "value" of converter() to "parameter".

The problem is when i debug and see then the parameter contains all deserialized xml but i don't know why "list" shows null always ? even i can see all the xml elements(i mean node values) on debugging but why "parameter" is not assigned to "list" ? (I guess it shows null because this parameter is of Parameter type. But how to make it use in current context ?)

Could some one please correct me and write me the way to equivalent of Converter() where we have already the object obtained from deserializing xml ? would be a big help.

Sss
  • 1,519
  • 8
  • 37
  • 67
  • 1
    `Parameter` is a instance of complex type, but you're trying to cast it to `ICollection`. How would you expect it to work? Does parameter class implements `ICollection`? – Sriram Sakthivel May 14 '14 at 07:46
  • @SriramSakthivel NO,It dont but do you know other way to achieve the converter() functionality given the object obtained from deserializd xml using this object i have to render UIElements. – Sss May 14 '14 at 07:48
  • It is not clear what you need. Are you looking for this? `ICollection list = new List { parameter };` – Sriram Sakthivel May 14 '14 at 07:51
  • @SriramSakthivel I have to implement the Converter() functionality without using inbuilt Converter() function. As you know its orginal structure is like this: "public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)" where "value "is actually object that contains the Parameter class object. So in current scenario i already have object obtained on deserializing xml which is "parameter" (from Parameter class, which is root class/element of my xml). – Sss May 14 '14 at 07:56
  • so what i have to do is to implement Converter() functionality , we already have "parameter" object and so we can do something like this : ICollection list = parameter as ICollection; List result = new List(); – Sss May 14 '14 at 07:57
  • If i was using inbuilt Converter() function it was like this : ICollection list = value as ICollection; List result = new List(); – Sss May 14 '14 at 07:58

1 Answers1

0

I found the solution of this finally actually there was no need to do :

ICollection<Parameter> list = parameter as ICollection<Parameter>;// **Problem creating line**
List<UIElement> result = new List<UIElement>(); 

we can directly use parameter object obtained from :

Parameter parameter = (Parameter)deserializer.Deserialize(reader);

and put if() conditions to access the desired xml elements/Class value(or you can say node) there is no need of creating list.

Sss
  • 1,519
  • 8
  • 37
  • 67