简体   繁体   中英

Get object reference from its instance variable in c#?

I am new to WPF application and also 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 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. 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!

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. If you're talking about the Image type that's built into .NET then there's no such property.

You will have to take steps to record the parent object of each Image somewhere else where you can get back to it later. 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.

If you follow the advice to put Image and myObject in a Dictionary together, you're going to have a bad time. 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:

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

您可以使用Tag属性存储有关Image实例的其他信息

Your Image class would have to have a Parent property that holds a reference back to your myObject instance. Then you'd have to make sure that when you set the image on myObject that you set the Parent property.

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. If you have a collection of myObject , then this is fairly easy with Linq, but the performance may not be great.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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