class Test
{
public BinaryWriter Content { get; private set; }
public Test Write (T data)
{
Content.Write(data);
return this;
Using Reflection considerably slows down method calls. It also moves argument type check from compile time to runtime, which is undesirable in most cases.
Using dynamic calls in this case is just a fancy and slightly optimized version of the previous method which has the same drawbacks.
The correct way is to copy all overloads:
public Test Write (bool data)
{
Content.Write(data);
return this;
}
public Test Write (byte data)
{
Content.Write(data);
return this;
}
public Test Write (byte[] data)
{
Content.Write(data);
return this;
}
public Test Write (char data)
{
Content.Write(data);
return this;
}
public Test Write (char[] data)
{
Content.Write(data);
return this;
}
// ...
This method is very verbose, but it is the only that provides compile-time checks of arument types and is the most performant as it chooses overloads and compile-time. Furthermore, these methods are likely to be inlined.
In cases like this, I usually use a T4 script which generates code like above based on Reflection. Create a TT file, enumerate Write overloads of BinaryWriter, generate code.