[英]Get object reference from its instance variable in c#?
I am new to WPF application and also c#. 我是WPF应用程序和C#的新手。 I'll explain my situation:
我将说明我的情况:
I have a class called myObject
which has an image instance variable of type Image
During runtime, I want to get the object reference of the image
like: 我有一个名为
myObject
的类,该类的图像实例变量的类型为Image
在运行时,我想获取image
的对象引用,例如:
myObject obj = new myObject();
Image img = obj.getImage();//reference to obj's image variable;
Now from img
i want to get the object obj
's reference. 现在从
img
我想获取对象obj
的引用。 How do I get this? 我怎么得到这个?
Edit: Below is a solution. 编辑:下面是一个解决方案。
Guys! 伙计们! First of all Thank you SO Much for all the help!
首先非常感谢您提供的所有帮助! I really appreciate!
我真的很感激! All of u rock!
你们都摇滚!
Following Noob.net 's advice, Whenever i create myObject obj, i stored the obj reference in img.Tag Property! 遵循Noob.net的建议,每当我创建myObject obj时,都会将obj引用存储在img.Tag属性中!
myObject obj = new myObject();
img.MouseLeftButtonDown += new MouseButtonEventHandler(img_MouseLeftButtonDown);
Image img = obj.getImage();
The handler part: 处理程序部分:
Image i = (Image)sender;
myObject otemp = (myObject)i.Tag;// refers to the same object from which img was created
The only way you can do this directly is if your Image
type has a reference back to it's owner object. 直接执行此操作的唯一方法是,如果您的
Image
类型具有对其所有者对象的引用。 If you're talking about the Image
type that's built into .NET then there's no such property. 如果您谈论的是.NET内置的
Image
类型,则没有此类属性。
You will have to take steps to record the parent object of each Image
somewhere else where you can get back to it later. 您将必须采取步骤将每个
Image
的父对象记录到其他位置,以便稍后再使用它。 One common pattern would be to maintain a HashTable
in memory where the Image
object is the key and the MyObject
object is the value. 一种常见的模式是在内存中维护一个
HashTable
,其中Image
对象是键,而MyObject
对象是值。
If you follow the advice to put Image
and myObject
in a Dictionary
together, you're going to have a bad time. 如果您遵循将
Image
和myObject
放到Dictionary
的建议,那将是一段糟糕的时光。 Your images will live as long as the dictionary does and probably will not be disposed of in a timely fashion. 您的图片将在字典中有效,并且可能不会及时得到处理。
So why track this mapping using a data structure when you can structure your data to track the reference by default ? 那么,为什么可以在结构化数据以默认跟踪引用时使用数据结构跟踪此映射?
If you wrap the mapping in a View Model , you'll see what I mean: 如果将映射包装在View Model中 ,您将明白我的意思:
public class MyObjectViewModel : INotifyPropertyChanged
{
private myObject obj;
public Image Image
{
get;
private set;
}
public MyObjectViewModel(myObject obj)
{
this.obj = obj;
this.Image = obj.getImage();
}
public void SomethingThatMakesImageChange()
{
this.Image = obj.getImage();
this.RaisePropertyChanged("Image");
}
// ... insert suitable INPC implementation ...
}
This approach has 2 distinct advantages: 这种方法有两个明显的优点:
Image
class will only live as long as necessary in the view Image
类将仅在视图中生存必要的时间 myObject
instances, you can use your View Model to handle these changes and updates to the Image
accordingly. myObject
实例的实时更改,则可以使用视图模型来处理这些更改并相应地更新Image
。 您可以使用Tag属性存储有关Image实例的其他信息
Your Image
class would have to have a Parent
property that holds a reference back to your myObject
instance. 您的
Image
类必须具有Parent
属性,该属性保存对myObject
实例的引用。 Then you'd have to make sure that when you set the image on myObject
that you set the Parent
property. 然后,您必须确保在
myObject
上设置图像时,要设置Parent
属性。
Now if you can't, or won't add a Parent
property to the Image
class, then your only option would be to compare instance of myObject
with the Image
to see if myObject
contains it. 现在,如果不能或不将
Parent
属性添加到Image
类,则唯一的选择是将myObject
实例与Image
进行比较,以查看myObject
包含它。 If you have a collection of myObject
, then this is fairly easy with Linq, but the performance may not be great. 如果您有
myObject
的集合,那么使用Linq相当容易,但是性能可能不佳。
I think I didnt understand, but here goes my answer: 我想我听不懂,但我的回答如下:
public class MyObject
{
private Image img;
public Image Img
{
get { return img; }
set { img = value; }
}
public MyObject()
{
Img = Image.FromFile("");
}
}
public class Main
{
MyObject obj = new MyObject();
//obj.Img = Image.FromFile(""); //if u want
Image image = obj.Img;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.