0

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. 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, i.e. Test myTest = new Test(); etc and I need to assign a class, say myResult (containing dozens of properties with values).

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 I.e.

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.

Could someone help me please with a working example?

Nick
  • 483
  • 1
  • 6
  • 15
  • Over half this code is irrelevant to what you are trying to ask here. Please update the post to include *only code necessary to* replicate your issue so we can help you out. `and after lots of things that got executed, a final task returns one (of hundreds..) or objects` what does this mean, I'm not seeing any of these things in code... – Trevor Feb 06 '20 at 19:57
  • @ Çöđěxěŕ Apologies, I just gave an example of what I have and where i need to go, if possible.. I don't have other code on this, trying to figure out how to do it.. Thank you for looking this up, appreciate. – Nick Feb 06 '20 at 19:59
  • no reason to apologize, it's fine :) I'm afraid that I am not understanding what your intentions are here. – Trevor Feb 06 '20 at 20:00
  • @ Çöđěxěŕ edited the description of the question as suggested. – Nick Feb 06 '20 at 20:01
  • @ Çöđěxěŕ I have many methods returning obj (classes), each different, say, myClassA, myObjectB etc with values inside. I am trying to assign the whole class into a property of a class called Test. Reason is, I tried to create properties of specific type, i.e. `public objectA myProperty1 {get; set;}`, then `public objectB myProperty2 {get; set;}` etc but are too many different types of classes (objectA, B.. etc) so I was wondering if I can declare a generic property like `public object myProperty {get; set;}` and then close there (one at a time of course) a whatever object I need... – Nick Feb 06 '20 at 20:07

3 Answers3

0

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

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.

Yennefer
  • 5,704
  • 7
  • 31
  • 44
  • Thank you all. To put some salt in the motive behind this: I do have many (more than 80) async tasks and each one returns an object. Actually, each one can return one of many objects, and are all Classes with linked classes with properties. For example, async Task A return objX12 (there are obj1 to 12) at one instance, and each object has different properties (although many are similar.. anyhow). So is extremely difficult to create a TestResults class with aaaall the properties and then depending the returning object (when each task finish..) assign the results from objX... in TestResults – Nick Feb 06 '20 at 20:14
  • Hence, I have a rather simple TestResults class and I am trying to put an object in a property, if feasible of course (I'm no expert..), hence I investigate a way to do it. Any way would be sufficient, as long as I can "reach" the name and value of the original property: for example, if I could assign somehow `myObjA` in property `TestResults.myData` (assuming myObjA has a property Age) I was hoping I could use `TestResults.myData.Age` etc. Hope there is a way.. Thank you! – Nick Feb 06 '20 at 20:18
0

I'd assume the simplest way of doing this is making your Test class generic:

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.

0

object is a class, like most other. 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:

  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. 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. 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. You will find the type object used for the sender in Event Handlers (this is 50% convetion, 50% usefull). 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.

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.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • I appreciate! I will have to do some thought-work on that but I think I understood your point. I have way too many classes that are different. So I was looking to, kind of, "assign" the collection of properties of any class that I need to a "generic"sort property. Kindly note, each class has linked classes so I would need months to go through a more straight forward way.. Imagine ClassA (with X properties, out of those Y properties are of type ClassB etc..) and there are values in many of those properties. So I need a way to "put" them as ClassA into a property.. – Nick Feb 06 '20 at 20:33
  • @Nick That thus far sounds like a boringly normal class. Class instances usually have a bunch of fields or properties of different types. Same way they have function with different Argument and Return value types. | If it is a 1:N realtionship, that usually calls for a `ClassB[]`, `List` or similar Collection. – Christopher Feb 06 '20 at 21:42