简体   繁体   中英

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) { ... }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM