Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to generate an object implementing different interfaces dynamically at runtime?

I'm looking at how to solve a problem and I'm not even sure this might be possible at all in C# & .NET 3.5:

Say I have a limited number of interfaces, each describing a specific, non-related set of methods. Now I have a number real-world devices which each may implement just a subset of these interfaces.

During set-up of comms with these devices they will tell me which capabilities they have. I would now like to create an object implementing the interfaces (each resembling one capability of the device) so that higher up in my application architecture I'm able to:

  • write code against the aforementioned interfaces
  • test if that generated object implements a certain interface to see if certain actions are supported

I'm not sure at all which approach to use towards this problem. Any comments or approaches most welcome!

like image 268
Timo Kosig Avatar asked Nov 03 '09 10:11

Timo Kosig


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Use a mocking framework such as Moq, RhinoMocks or TypeMock Isolator

If you're looking to do something lower level, things like Castle DynamicProxy might be a good direction for you.

like image 52
Ruben Bartelink Avatar answered Sep 24 '22 14:09

Ruben Bartelink