- I have a generic interface that requires a read and key method. I want to use reflection to get an instance of each implementation.
- Once I have a the implementation, I want to get it's key and store the key and the instance in a dictionary.
- Eventually, I would have a method where I pass in a key and byte[], it would look the key up in the dictionary and use the instance that it retrieves to read the byte[] and return an
object
. - I hope this is understandable, if not I can answer questions in the comments.
- This example only has int and string, but I'll have many more types in my real implementation.
The Goal
To have a dictionary with 2 entries stored as such:
- 43,
IRead<int> new Reader()
- 61,
IRead<string> new Reader()
public interface IRead<T>
{
T Read(bool[] binary);
int Key( T type );
}
public class Reader : IRead<int>, IRead<string>
{
int IRead<int>.Read( byte[] binary )
{
// does stuff
returns 4;
}
public int Key( int type ) { return 43; }
string IRead<string>.Read( byte[] binary )
{
// does stuff
returns "example";
}
public int Key( string type ) { return 61; }
static StreamProcessor( )
{
// Here I want to get the definitions
}
}