简体   繁体   English

从PropertyInfo获取对象引用

[英]Get object reference from PropertyInfo

I have a little problem, below my code: 我的代码下面有一个小问题:

public class A
{
    public string ObjectA { get; set; }
}

public void Run()
{
    A a = new A();
    a.ObjectA = "Test number 1";

    BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
    PropertyInfo myPropertyInfo = a.GetType().GetProperty("ObjectA", flags);
    object myNewObject = myPropertyInfo.GetValue(a);// Here should be reference

    a.ObjectA = "Test number 2";//After this line value myNewObject continued "Test number 1"
}

So my value myNewObject must be in output "Test number 2". 因此,我的值myNewObject必须在输出“测试编号2”中。 Is there any way? 有什么办法吗? It is this at all possible? 这有可能吗?

Wrong! 错误!

You're getting the string rather than the instance of A using reflection. 您正在获取字符串,而不是使用反射的A实例。

Changing A.ObjectA doesn't change the string reference. 更改A.ObjectA不会更改字符串引用。 Actually, you're setting a different string to the backing string class field by the ObjectA property... 实际上,您是通过ObjectA属性为后备字符串类字段设置不同的字符串...

Auto-properties are syntactic sugar to avoid explicitly declaring class fields to properties which perform nothing when getting or setting their values: 自动属性是一种语法糖,可以避免将类字段显式声明为在获取或设置其值时不执行任何操作的属性:

// For example:
public string Text { get; set; }

// is...
private string _text;
public string Text { get { return _text; } set { _text = value; } }

Now turn your code into regular one (no reflection): 现在将您的代码转换为常规代码(无反射):

A a = new A();
a.ObjectA = "hello world";

object myNewObject = a.ObjectA;

// Do you think now that myNewObject should change...? :)
a.ObjectA = "goodbye";

Is there any way? 有什么办法吗? It is this at all possible? 这有可能吗?

No. 没有。

Maybe you can simulate this behavior using a Func<T> ... 也许您可以使用Func<T>来模拟这种行为。

Func<object> myNewObjectGetter = () => myPropertyInfo.GetValue(a);

Now, whenever you call myNewObjectGetter() you're going to get the most fresh value of the whole property. 现在,每当调用myNewObjectGetter()您将获得整个属性的最新鲜的价值。 BTW, this still doesn't address the impossible! 顺便说一句,这仍然没有解决不可能!

Is there any way? 有什么办法吗?

No. You can't put a reference to a property into an object variable. 不能。您不能将对属性的引用放入对象变量中。 Such a variable can only hold a normal value, such as the string you put into it. 这样的变量只能保存一个普通值,例如您放入其中的字符串。

That answers the question as asked. 这回答了所问的问题。 You can clarify what you want to achieve and maybe we can suggest a better way. 您可以阐明要实现的目标,也许我们可以建议一种更好的方法。

Probably there is no solution but I show some code 可能没有解决方案,但我显示了一些代码

public class MyRows
{
    public string Key { get; set; }
    public int Id { get; set; }
    public object Val { get; set; }
}

public abstract class BasicDTO
{
    public int? Id { get; private set; }

    public PropertyInfo[] PropertyDTO;
    protected Type myType;

    public BasicDTO()
    {
        Load();
        BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
        PropertyDTO = myType.GetProperties(flags);
    }
}

public class CustomerDTO : BasicDTO
{
    public string Surname { get; set; }
    public string Phone { get; set; }

    public CustomerDTO() { }

    protected override void Load()
    {
        myType = typeof(CustomerDTO);         
    }
}

Now my basic method 现在我的基本方法

public void Run(BasicDTO dto)
{
    PropertyInfo pi = dto.PropertyDTO.Where(x => x.Name == "Surname").SingleOrDefault();
    MyRows mr = new MyRows();
    mr.Val = pi.GetValue(dto);//Here I need reference
}

When I change CustomerDTO.Surname my value mr.Val it must also be changed. 当我更改CustomerDTO.Surname我的值mr.Val ,也必须更改它。 As I wrote above, it is probably impossible, but maybe anybody have a idea. 正如我在上面所写,这可能是不可能的,但也许任何人都有一个主意。

BTW: Value mr.Val I use only for binding (WPF). 顺便说一句:值mr.Val我只用于绑定(WPF)。 So maybe you have any other suggestions, how solve problem. 因此,也许您还有其他建议,如何解决问题。 I will be grateful for your help 我将感谢您的帮助

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

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