tags:

views:

67

answers:

2
Paragraph p = new Paragraph();

void Function(var inline)
{
    var r = (inline);
    string rSer = XamlWriter.Save(r);
    var inl1 = XamlReader.Parse(rSer);
    p.Inlines.Add(inl1); // error The best overloaded method match for System.Windows.Documents.InlineCollection.Add(System.Windows.UIElement)' has some invalid arguments    
}

inline type can be System.Windows.Documents.Run or System.Windows.Documents.Span.

How do I know the type of inline and lead to it?

I need something like this:

Type t = Type.GetType(inline.GetType().ToString()); // results in t == null 
p.Inlines.Add(inline as t);
+2  A: 

It would be easiest to just test and cast.

if (inl1 is Run)
    p.Inlines.Add((Run)inl1);
else if (inl1 is Span)
    p.Inlines.Add((Span)inl1);

edit:
I think I understand your thought process. It doesn't matter if your object is casted to an Inline. Its underlying type is still what you expect. This is for the compiler's benefit to know which methods to call on your behalf. When you use XamlReader.Parse(), it returns the appropriately typed item as an object reference. The InlineCollection that you're adding to is not expecting an object and that is why it fails. Since the types are actually Run or Span which both inherit from Inline (which the collection is expecting), you need to use the appropriately typed variables.

With all that in mind understanding your mindset, I think your function can be reduced to simply:

void Function(Inline inline)
{
    p.Inlines.Add(inline);
}
Jeff M
I do want to avoid
simply denis
+1  A: 

Since System.Windows.Documents.Run and System.Windows.Documents.Span both derive from System.Windows.Documents.Inline it could be as simple as:

// untested
void Function(Inline inline1)
{
    string rSer = XamlWriter.Save(inline1);
    var inline2 = XamlReader.Parse(rSer) as Inline;
    p.Inlines.Add(inline2); 
}
Henk Holterman
it works, but lost the properties of objects.
simply denis
@simply, If it works (ie the output is right) then the properties are all there.
Henk Holterman
@Henk Holterman Yes, it works, the problem turns out to be a friend. for this http://stackoverflow.com/q/3793830/450466
simply denis