简体   繁体   English

我如何从属性中获取实例

[英]how can I get instance from the property

In my application I have a class which has properties of user-defined types like this: 在我的应用程序中,我有一个类,它具有用户定义类型的属性,如下所示:

class MyType
{
    public A MyProperty
    {
        get;
        set;
    }
}
class A
{
    .....some methods and proeprties
}

for some operations that I need to perform from my main program, I have created a List of A and add in to it MyProperty whenever creating object of MyType 对于需要从主程序执行的某些操作,我创建了一个A列表,并在创建MyType对象时将其添加到MyProperty中。

List<A> myList = new List<A>();

MyType m1 = new MyType();
myList.Add(m1.MyProperty);

MyType m2 = new MyType();
myList.Add(m2.MyProperty);

......more instances

and pass it to my main program and there I perform different operation on these properties which reflects in there instances also. 并将其传递给我的主程序,在那里我对这些属性执行不同的操作,这也反映在这些实例中。 Is there any way by which I could get the object instance for any particular MyProperty from that property in the list. 有什么方法可以从列表中的该属性获取任何特定MyProperty的对象实例。

I suspect you mean you're creating a List<A> and you want to be able to find out from an instance of A which instance of MyType "owns" it. 我怀疑您的意思是您正在创建List<A>并且希望能够从A的实例中找出MyType A哪个实例“拥有”它。 There's no way of doing that without putting the information in A (or having a comprehensive list of instances of MyType and checking them). 没有将信息放入A (或没有完整的MyType实例列表并对其进行检查),就无法做到这一点。 After all, two instances of MyType could have the same A : 毕竟, MyType两个实例可以具有相同的A

MyType first = new MyType();
MyType second = new MyType();
A a = new A();
first.MyProperty = a;
second.MyProperty = a;

Which MyType is a logically associated with? 哪个MyType a逻辑上与之关联? Both or neither, really... 两者皆有或两者皆有,真的...

The cleanest approach is to make A know about which MyType it's associated with explicitly - then you can decide what should happen if you try to have one instance of A associated with two instances of MyType (should it throw an exception? Keep both as a list? Keep the newer?) You probably want to make the MyType.MyProperty update the value that's passed in, to keep the association in sync. 最干净的方法是让A知道它与哪个MyType显式相关-然后,如果您尝试将A的一个实例与MyType两个实例相关联,则可以决定会发生什么(应该抛出异常吗?将两者都保留为列表) ?保持更新?)您可能想使MyType.MyProperty更新传入的值,以使关联保持同步。

The simlest answer: use Dictionary, not List. 最简单的答案:使用字典,而不是列表。 But I think its ugly. 但是我觉得它很丑。 Or put in A internal parent referense. 或放入内部父参照。

class A:ICloneable
{
    internal MyClass _parent;

    public MyClass Parent
    {
        get
        {
            return this._parent;
        }
    }

#region ICloneable Members

    public object Clone()
    {
        return this.MemberwiseClone();
    }

#endregion

    .....some methods and proeprties
}

class MyType
{
    private A _myProperty;

    public A MyProperty
    {
        get
        {
                return this._myProperty;
        }
        set
        {
                this._myProperty = a.Clone();
                this._myProperty._parent = this;
        }
    }
}

Important! 重要! Read about MemberwiseClone Function 阅读有关MemberwiseClone函数的信息

Any reason why you could not do this instead:- 您为何无法执行此操作的任何原因:-

List<MyType> myList = new List<MyType>();

MyType m1 = new MyType();
myList.Add(m1);

And then in the main program you can get all of the MyProperty's from the MyType plus you would have access to any info on the MyType object which you may need. 然后在主程序中,您可以从MyType获取所有MyProperty,此外,您还可以访问可能需要的有关MyType对象的任何信息。

The idea is a little similar to this...I often see developers create a method like this 这个想法有点类似...我经常看到开发人员创建这样的方法

public void ProcessFoo(int idOfFoo){}

then later on they need other details from the Foo object that they add to the signature 然后,他们需要从Foo对象中获取其他细节,并将其添加到签名中

public void ProcessFoo(int idOfFoo, string name){}

The best approach (unless there is some weird limitation) is just pass in the whole Foo object in the first place... 最好的方法(除非有一些怪异的限制)只是首先传递整个Foo对象。

public void ProcessFoo(Foo foo){}

hmmm well I hesitate to give any extra 'advice' without more context. 嗯,我很乐意在没有更多上下文的情况下提供任何其他“建议”。 I'm sure within the context of what you are doing it makes sense but it seems hazardous to me. 我敢肯定,在您正在做的事情中,这是有道理的,但对我来说似乎很危险。

Jon's answer is very relevant with regard to one instance of A could be assigned to the MyProperty of MyType, then what you're saying is that instance of A could also belong to MyPropertyX and MyPropertyY. 乔恩的答案与A的一个实例可以分配给MyType的MyProperty有关,因此,您要说的是A的实例也可以属于MyPropertyX和MyPropertyY。

Sorry stremlenye but what is the purpose of cloning the object? 对不起stremlenye,但是克隆对象的目的是什么? To me there would need to be a very good reason for creating a whole new separate instance...especially since cloning is rarely implemented well on all but the simplest of objects (perhaps there is a good reason, I just can't see it from the example). 对我来说,创建一个全新的单独实例将是一个很好的理由……尤其是因为除了最简单的对象之外,很少能在所有对象上很好地实现克隆(也许有一个很好的理由,我只是看不见它)从示例)。

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

相关问题 如何从Unity获取接口实例? - How can I get the instance of a interface from Unity? 如何从当前执行的方法获取BackgroundWorker的实例? - How can i get the instance of a BackgroundWorker from the currently executed method? 如何在没有具体实例的情况下使用Reflection来获取Type的静态属性的值 - How can I use Reflection to get the value of a Static Property of a Type without a concrete instance 如何通过C#中的参数获取属性的对象实例? - How can I get a property's object instance through a parameter in C#? 如何使用 Pupeetersharp 从元素中获取计算的样式属性 - How can I get computed style property from element with Pupeetersharp 如何从匿名类型获取属性的值? - How can I get a value of a property from an anonymous type? 如何从表达式树中获取属性名称? - How can i get property names from an Expression tree? 如何从该导航属性获取值? - How can I get a value from this navigation property? 如何从集合中获取一个属性并将其分配给数组? - How can I get one property from a collection and assign it to an array? 如何读取提升为二进制的工作流实例属性? - How can I read a workflow instance property promoted as binary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM