简体   繁体   English

C#:延迟加载,委托 Class,反射

[英]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.实现 IMyInterface 并从基础 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.返回某些方法/属性的一些预定义值,并在访问其他方法/属性时通过委托创建 MyClass 的实例。 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.通过反射检查 MyClass(或 MyLazyLoadingClass 代理对象)以确定/获取/设置属性/自定义属性。 Because MyLazyLoadingClass is not derived from the ABaseClass, it does not contain the same properties/methods/custom/attributes.因为 MyLazyLoadingClass 不是从 ABaseClass 派生的,所以它不包含相同的属性/方法/自定义/属性。

Is there a way to tell Reflection it should use the MyClass instance when MyLazyLoadingClass is inspected?有没有办法告诉 Reflection 在检查 MyLazyLoadingClass 时它应该使用 MyClass 实例? or Is it possible to 'replace' the MyLazyLoadingClass instance with the loaded MyClass instance?或者是否可以用加载的 MyClass 实例“替换” MyLazyLoadingClass 实例? (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.但是您可能希望在运行时创建MyLazyLoadingClass ,而是通过向其添加方法等方式从ABaseClass “派生”它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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