1

I have:

class A
{
 public object obj1 {get;set;}      
 public object obj2 {get;set;}      
 public object obj3 {get;set;}      
}

Somewhere i have instance of obj2 for example. How can i get its name ("obj2") without using class A or instance A?

3
  • What do you mean by "Somewhere i have instance of obj2"? Can you show some code? Commented Oct 14, 2011 at 7:04
  • 2
    What do you mean by its name? Suppose that obj1,2 and 3 are all the same object What is its name? is it "obj1" or "obj3"? You could use reflection to get the field name of any field whose value equals some object. But this is not its name, nor is it unique. Is this what you want? Commented Oct 14, 2011 at 7:05
  • For example i call method for another class and send obj2 as parameter. obj1, obj2, obj3 different instance of one class Commented Oct 14, 2011 at 7:07

3 Answers 3

2

You can't do this. obj2 is the name of a property of the class A. So without using class A you cannot know its property names.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok. How can i do this, if i have A
@Famos, you could use reflection to get a list of properties of A. But I am not quite sure what you are trying to achieve exactly here.
I using PostSharp for observe changes and i need know name object what contain changed value
1

The only way is tracking the build process of class A, example:

public class Container
{
    Dictionary<string, object> _objectContainer;

    public Container()
    {
        _objectContainer = new Dictionary<string, object>();
    }

    public void SetByName( string name, object obj )
    {
        if( !_objectContainer.ContainsKey( name ) )
        {
            _objectContainer.Add( name, obj );
        }
        else
        {
            _objectContainer[name] = obj;
        }
    }

    public object GetByName( string name )
    {
        return _objectContainer[name];
    }
}

public class ABuilder
{
    public static A Build( Container container )
    {
        var a = new A();

        var obj1 = new object();
        var obj2 = new object();
        var obj3 = new object();

        container.SetByName( "obj1", obj1 );
        container.SetByName( "obj2", obj2 );
        container.SetByName( "obj3", obj3 );

        a.Obj1 = obj1;
        a.Obj2 = obj2;
        a.Obj3 = obj3;

        return a;
    }
}

class Program
{
    static void Main( string[] args )
    {
        var container = new Container();

        var obj = ABuilder.Build( container );

        var obj1 = container.GetByName( "obj1" );
    } 
}

2 Comments

I think about it, if simple way not exist
ok, but your question is "Somewhere i have instance of obj2 for example. How can i get its name ("obj2") without using class A or instance A?" nothing about easy way... my solution works and it's an answer to your question.
0

To do this without the instance of class A, you have to modify the type of your properties :

class A
{
  private B obj1, obj2;

  public B Obj1 
  { 
    get { return obj1; }
    set 
    {
      if (value != null && value.Owner != null)
        throw new ArgumentException();
      if (obj1 != null)
        obj1.Owner = null;
      obj1 = value;
      obj1.Owner = "Obj1";
    }
  }
  public B Obj2
  {
    get { return obj2; }
    set
    {
      if (value != null && value.Owner != null)
        throw new ArgumentException();
      if (obj2 != null)
        obj2.Owner = null;
      obj2 = value;
      obj2.Owner = "Obj2";
    }
  }
}

class B
{
  public string Owner { get; internal set; }
  // ...
}

1 Comment

If you want to avoid hardcoding "Obj1" you may try with MethodInfo.GetCurrentMethod().Name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.