开发者

C#: Lazy Loading, Delegate Class, Reflection

I have the following scenario:

interface IMyInterface { ... } 

Defines a common interface with properties, methods etc. .

class MyClass : ABaseClass, IMyInterface { ... } 

Implements IMyInterface and is derived from a base class.

class MyLazyLoadingClass : IMyInterface { ... } 

Returns some predefined values for some methods/properties and creates an instance of MyClass via a delegate when other methods/properties are accessed. It also defines the following conversion:

implicit operator MyClass(MyLazyLoadingClass myLazyLoadingClass) {开发者_如何学Go ... }

Which creates the instance and returns it. (Remark: I know an implicit conversion should not involve such operations but due to the existing system design I found no other solution.)

Basically the implementation works. My Problem:

MyClass (or the MyLazyLoadingClass proxy object) is inspected via reflection to determine/get/set properties/custom attributes. Because MyLazyLoadingClass is not derived from the ABaseClass, it does not contain the same properties/methods/custom/attributes.

Is there a way to tell Reflection it should use the MyClass instance when MyLazyLoadingClass is inspected? or Is it possible to 'replace' the MyLazyLoadingClass instance with the loaded MyClass instance? (I don't really think so).


No, I don't think you can do that.

But you might want to create MyLazyLoadingClass at runtime instead, having it "derive" from ABaseClass by adding methods to it etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜