简体   繁体   English

将对象分配给对象类型的属性

[英]Assigning an object to a Property of type object

After lots of looking around (including SIMILAR but not same questions), I hope my question will make sense and someone could help me resolve this.经过大量环顾四周(包括类似但不相同的问题),我希望我的问题有意义并且有人可以帮助我解决这个问题。

I have many classes, say ClassA, ClassB, ClassC etc and they all exist for specific reasons.我有很多类,比如 ClassA、ClassB、ClassC 等,它们都是出于特定原因而存在的。 I have another Class which is being used for testing purposes and this includes two properties, set as follows:我有另一个用于测试目的的类,它包括两个属性,设置如下:

  public class Test : Countable<TestScenario>
  {


    //test completed
    public bool completed { get; set; } = false;

    //break or error (stop further execution) or not
    public bool breakOnError { get; set; } = true;

    //test result
    public bool success { get; set; } = false;

    //what did we test
    public string testCase { get; set; } = string.Empty;

    //what data we got as result
    public object testDataResult { get; set; }

    public Test()
    {
    }

   }

My problem is this: I instantiate the class, ie Test myTest = new Test();我的问题是:我实例化了这个类,即 Test myTest = new Test(); etc and I need to assign a class, say myResult (containing dozens of properties with values).等等,我需要分配一个类,比如 myResult (包含数十个带有值的属性)。

I need to do (something like) this:我需要做(类似)这样的事情:

    Test myTest = new Test();
     myTest.testDataResult = myResult;

It is safe to assume that later, if for example there's a myResult.SomeProperty with value 18, I would like to see it from myTest.testDataResult.SomeProperty Ie可以安全地假设稍后,例如,如果有一个值为 18 的 myResult.SomeProperty,我希望从 myTest.testDataResult.SomeProperty 即看到它

Console.WriteLine(myTest.testDataResult.SomeProperty.ToString());
//18

Is this possible?这可能吗? Being looking all around Binaryformatters and Reflection, one example seemed also good ( https://www.codeproject.com/Articles/1111658/Fast-Deep-Copy-by-Expression-Trees-C-Sharp ) mentioned as link here in SO ( Faster deep cloning ) but I couldn't make it, is above my experience level.在四处寻找 Binaryformatters 和 Reflection 时,一个例子似乎也不错( https://www.codeproject.com/Articles/1111658/Fast-Deep-Copy-by-Expression-Trees-C-Sharp )在 SO 中作为链接提到( 更快的深度克隆)但我做不到,超出了我的经验水平。

Could someone help me please with a working example?有人可以帮我举一个有效的例子吗?

If I understood correctly, it seems to me you are mixing up things.如果我理解正确的话,在我看来你是在混淆事物。

When you assign something to object, you are saying that you are putting your instance in a box which has no outstanding type.当你为对象分配一些东西时,你是说你把你的实例放在一个没有突出类型的盒子里。 This means that when you want to read that instance, you first have to cast it to the correct type in order to access the properties.这意味着当您想要读取该实例时,您首先必须将其转换为正确的类型才能访问属性。

You have to do something like this:你必须做这样的事情:

(SomeClss() myTest.testDataResult).Property1 (SomeClss() myTest.testDataResult).Property1

For this reason, you should try to introduce a type, which could be an interface, to describe the minimum set of methods and properties that you require.出于这个原因,您应该尝试引入一种类型(可以是接口)来描述所需的最少方法和属性集。 If you can't, no problem, you just have to cast.如果你不能,没问题,你只需要投射。 Keep in mind that the latter approach may crash at runtime if you choose the incorrect type, while the former one allows you to spot mistakes at compile time.请记住,如果您选择了不正确的类型,后一种方法可能会在运行时崩溃,而前一种方法可以让您在编译时发现错误。

I'd assume the simplest way of doing this is making your Test class generic:我认为最简单的方法是使您的 Test 类通用:

public class Test<TResult> : Countable<TestScenario>
{
  // ... the test outcome properties listed above
  public TResult testDataResult { get; set; }
  // ... constructor
}

That will allow you to access all the properties of your test data result instances without any black magic.这将允许您访问测试数据结果实例的所有属性,而无需任何魔法。

object is a class, like most other. object 是一个类,就像大多数其他类一样。 You can inherit from it, instantiate objects of it (but you only do that rarely), declare a variable of it's type.你可以继承它,实例化它的对象(但你很少这样做),声明一个它的类型的变量。 There are however 2 special rules owing to it's singular historic/design position:然而,由于其独特的历史/设计地位,有 2 个特殊规则:

  1. Every single class implicitly or explicitly inherits from object.每个类都隐式或显式继承自对象。 If you define no baseclass, object is appplied implicitly.如果未定义基类,则隐式应用对象。 If you define a baseclass, you just have to go back far enough it's inheritance chain and you will find object.如果你定义了一个基类,你只需要回到足够远的继承链,你就会找到对象。 The type object is the root of every class heirarchy.类型对象是每个类层次结构的根。

  2. While Numeric Types, structs and co do not inherit from Object, they can be boxed into object.虽然数字类型、结构体和 co 不继承自 Object,但它们可以装箱到 object 中。 It looks very similar, but there are differences in details.它看起来非常相似,但在细节上存在差异。

As a result every single imaginable type in .NET can be assigned to a variable of type object.因此,.NET 中每个可以想象的类型都可以分配给类型为 object 的变量。 It either is a object anyway so inheritance takes over, or it can be boxed into one.它要么是一个对象所以继承接管,要么它可以被装箱成一个。 Now we do not use object as a type a lot anymore.现在我们不再使用对象作为类型了。 While you can assign anything to object, you loose the compile time type checks while doing so.虽然您可以为对象分配任何内容,但这样做时会丢失编译时类型检查。 And that is a very important thing to loose.这是一件非常重要的事情。 I would never sacrifice it, if I can help it.如果我能帮助它,我永远不会牺牲它。

Generics vs object泛型与对象

Using object as type has fallen out of favor with generics.使用对象作为类型已经不再受泛型的青睐。 The lock statement is one of the rare cases where you still create a variable of type object and explicitly create a isntance of object. lock 语句是您仍然创建对象类型的变量并显式创建对象的实例的罕见情况之一。 You will find the type object used for the sender in Event Handlers (this is 50% convetion, 50% usefull).您将在事件处理程序中找到用于sender的类型对象(这是 50% 的对流,50% 的有用)。 Pretty much everywhere else we use generics.几乎所有其他地方我们都使用泛型。

Generics have been added to the Framework and language specifically to avoid having to resort to object as a variable type anymore.泛型已专门添加到框架和语言中,以避免再将对象作为变量类型。 And I picked "resort to" very intentionally.我非常有意地选择了“诉诸于”。 The Generic Collections like List<T> , Queue<T> and Dictionary<TKey,TValue> replaced the pre-generic ones like Queue, HashTable and wichever one we used instead of list.List<T>Queue<T>Dictionary<TKey,TValue>这样的通用集合取代了像 Queue、HashTable 这样的前通用集合,以及我们使用的任何一个来代替列表。

We can not really figure out what your goal is.我们无法真正弄清楚您的目标是什么。 However it does sound a lot like a XY Problem, simply because using object as a variable type is part of it.然而,这听起来很像 XY 问题,仅仅是因为使用对象作为变量类型是其中的一部分。

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

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