简体   繁体   English

C#引用属性的类的名称

[英]C# Name of class that references a property

Is there anyway to pass an object and get back the object that holds a reference to it? 无论如何传递一个对象并获取包含它的引用的对象?

Example: 例:

public class Person
{
    public string Name { get; set; }

    public Person(string name)
    {
        this.Name = name;
    }
}

public static class Helper
{

    public static void IsItPossible()
    {
        var person = new Person("John Doe");

        var whoKnowsMe = WhoIsReferencingMe(person.Name);

        //It should return a reference to person
    }

    public static object WhoIsReferencingMe(object aProperty)
    {
        //The magic of reflection
        return null;
    }
}

The code here is dumb. 这里的代码是愚蠢的。 But I'll be using to simplify DataBinding in a Windows Form solution. 但我将用于在Windows窗体解决方案中简化DataBinding。

Here is where I'll use it: 这是我将使用它的地方:

    protected void Bind(object sourceObject, object sourceMember, 
        Control destinationObject, object destinationMember)
    {
        //public Binding(string propertyName, object dataSource, string dataMember);
        string propertyName = GetPropertyName(() => destinationMember);
        string dataMember = GetPropertyName(() => sourceMember);

        Binding binding = new Binding(propertyName, sourceObject, dataMember);

        destinationObject.DataBindings.Add(binding);
    }

    public  string GetPropertyName<T>(Expression<Func<T>> exp)
    {
        return (((MemberExpression)(exp.Body)).Member).Name;
    }

The reason is that the function is kind of redundant: 原因是该功能有点多余:

    this.Bind(viewModel.Client, viewModel.Client.Id, view.icClientId, tiew.icClientId.Text);

I ask in order to simplify it to this: 我问为了简化它:

    this.Bind(viewModel.Client.Id, view.icClientId.Text);

So... any chance of this happening? 那么......发生这种情况的机会是什么? Or there's a simpler way of binding that I'm unaware of? 或者有一种我不知道的更简单的绑定方式?

Is there anyway to pass an object and get back the object that holds a reference to it? 无论如何传递一个对象并获取包含它的引用的对象?

No, in general. 不,一般而言。 There may be ways of doing this if you're using the debugger API, but only for debugging purposes. 如果您正在使用调试器API, 可能有一些方法可以执行此操作,但用于调试目的。 Your production design shouldn't require it. 您的生产设计不应该要求它。

You could potentially use expression trees instead though: 您可以使用表达式树来代替:

this.Bind(() => viewModel.Client.Id, () => view.icClientId.Text);

... and work out from the expression tree both the originating object and the property that it's using. ...从表达式树中找出原始对象和它正在使用的属性。

Is there anyway to pass an object and get back the object that holds a reference to it? 无论如何传递一个对象并获取包含它的引用的对象?

No, it's not possible as built-in feature. 不,它不可能作为内置功能。 You have to architect it in your code. 您必须在代码中构建它。 Object as itself has no knowledge about references that point to it. 对象本身并不了解指向它的引用。 This is a GC duty, to take trace of this. 这是一项GC职责,需要对此进行跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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