I am using a type derived from DynamicObjectas a builder for some strings. In the end, I call ToStringto get the final result.
At that moment, I thought it would give me a normal string, but this string is somehow weird. It behaves like one when I use string functions on it, but it behaves as if I don't know what, that neither a string nor a speaker.
This is how I implemented ToStringon 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 as follows
Console.WriteLine(example.ToString().Extension());
Where
static class Extensions
{
public static string Extension(this string str)
{
return str.ToUpper();
}
}
application crashes using RuntimeBinderExceptionspeaking
'string' does not contain a definition for 'Extension'
,
Console.WriteLine(((string)example.ToString()).Extension());
, .
Console.WriteLine((string)example);
Console.WriteLine(example);
, , .
, - ?