简体   繁体   English

从对象实例在C#中获取对象引用?

[英]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. 如果您遵循将ImagemyObject放到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: 这种方法有两个明显的优点:

  1. Your View Models typically only live as long as your model does, which means the heavy Image class will only live as long as necessary in the view 您的视图模型通常只能在模型中生存,这意味着沉重的Image类将仅在视图中生存必要的时间
  2. If you need to adapt to real time changes to 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.

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