C#反思:是否有可能找到在运行时对象的实例? [英] C# Reflection: Is it possible to find an instance of an object at runtime?

查看:122
本文介绍了C#反思:是否有可能找到在运行时对象的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否有可能使用反射来查找在运行时的对象?这更多的是一种实验比实际需要的。

I'm wondering if it's possible to use reflection to locate an object at runtime? This is more of an experiment than a practical requirement.

我用.GetType()方法上的一个对象实例做各种事情的对象,但我的问题是:如果我知道某个类型的对象存在在运行时,但我不能按名称引用它

I've used the .GetType() method on an object instance to do various things to the object, but my question is: what if I know an object of a certain type exists at runtime, but I can't refer to it by name.

作为一个更具体的例子,假设我有一个WinForms应用程序加载一个DLL - 如何可能是DLL独立,以与表单交互,或者叫公共方法

As a more concrete example, suppose I have a WinForms app that loads a DLL - how might that DLL independently locate a reference to the form object in order to interact with the form, or call a public method?

这甚至可能吗?

推荐答案

没有,基本上是这样。

你可能有某种可怕的世界地图,从类型的类型的实例我很感兴趣,但除此之外,没有。

You could have some sort of horrible global map from type to "the instance of the type I'm interested in" but otherwise, no.

基本上,WinForms应用程序应该传递到形式进入不知何故DLL的引用。

Basically, the WinForms app should pass a reference to the form into the DLL somehow.

这篇关于C#反思:是否有可能找到在运行时对象的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆