How can a class be designed to mock an unknown type?

I saw examples of this where the interface is provided by the factory, and it generates objects that implement the interface. It is not so difficult for me, but I do not understand how it can be used to implement behavior.

Take ChannelFactory in WCF, for example ... when you create a channel from an interface, it provides methods (from the interface) that invoke remote procedures when called. It seems that I have a small gap in my knowledge of how this can be done. This is probably the general design pattern, but I decided that I would use SO again as an extension of the study.

+3
source share
2 answers

. . System.Reflection.Emit.

, Type , , , , TypeBuilder, , ( / return default (T)), MethodBuilder .., .

+1

Mocking frameworks, , , . () . , , .

:

Mock<IRepository> repMock = new Mock<IRepository>();
repMock.Setup(r => r.FindById(1)).Returns(new MyObject());

, IRepository FindByID, Setup "" . Reflection.Emit, IL .

+1

Source: https://habr.com/ru/post/1762949/


All Articles