I use a type derived from a DynamicObject
as a builder for some strings. At the end I call ToString
to get the final result.
At this point I thought it would give me a normal string but this string is somehow strange. It behaves like one when I use string functions on it but it behaves like I don't know actually what, something neither a string nor a dynamic.
This is how I implemented ToString
on my builder
public class Example : DynamicObject
{
public override bool TryConvert(ConvertBinder binder, out object result)
{
if (binder.ReturnType == typeof(string))
{
result = ToString();
return true;
}
result = null;
return false;
}
public override string ToString()
{
return base.ToString();
}
}
When I run it like this
dynamic example = new Example();
Console.WriteLine(example.ToString().ToUpper());
the result is correct: USERQUERY+EXAMPLE
(when executed in LINQPad)
However if I call the second line like this
Console.WriteLine(example.ToString().Extension());
where
static class Extensions
{
public static string Extension(this string str)
{
return str.ToUpper();
}
}
the application crashes with a RuntimeBinderException
saying
'string' does not contain a definition for 'Extension'
but if I cast the result it works again
Console.WriteLine(((string)example.ToString()).Extension());
Maybe one more example.
Console.WriteLine((string)example); // UserQuery+Example
but
Console.WriteLine(example); // DynamicObject UserQuery+Example
You can actually never be sure what you'll get until you cast it to string.
Why is this happening and is there a way to avoid the additional cast and get somehow a real string?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…