Korexio
Korexio

Reputation: 483

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

Upvotes: 0

Views: 702

Answers (1)

Daren Thomas
Daren Thomas

Reputation: 70324

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.

Upvotes: 1

Related Questions