3

So, I have the following structure:

public abstract class MyBase
{
    public Type TargetType { get; protected set; }
}

public class A : MyBase
{
    public A()
    {
        TargetType = GetType();//Wrong, I need B class type not C
    }
}

public class B : A
{
    public B() { }
}

public class C : B
{
    public C() { }
}

Of course, I can receive my type in this way:

public class B : A
{
    public B()
    {
        TargetType = typeof(B);
    }
}

Actually, I have to write some code to make the example clearer:

Class1.cs

public static Dictionary<Type, Type> MyTypes = new Dictionary<Type, Type>()
{
    { typeof(B),typeof(BView) }
}

public Class1()
{
    C itemC = new C();
    Class2.Initialize(itemC);
}

Class2.cs

public static Initialize(MyBase myBase)
{
    Type t;
    Class1.MyTypes.TryGetValue(myBase.TargetType, out t);
    //I need get BView but I get null because *myBase.TargetType* is C class type
}

Level structure:

  • Level 0:(MyBase) - 1 object
  • Level 1:(A) - 2 objects
  • Level 2:(B) - 100 objects and more
  • Level 3:(C) - 80 objects and more

I gave this case in brackets

I will be grateful for any help

Mr Luk
  • 53
  • 6
  • 1
    Can you please explain better what you want to achieve? – Francesco Bonizzi Apr 22 '18 at 19:58
  • 1
    Can you please explain what you are trying to achieve? Your example isn't clear at all. – Federico Dipuma Apr 22 '18 at 19:58
  • I inherited after A about 100 different objects. So I need find derived type in A class (In this case B class) – Mr Luk Apr 22 '18 at 20:06
  • System.Type has a 'BaseType'-Property, have you tried that ? You can call GetType().BaseType in a C-Instance and this should return B-Type – D.J. Apr 22 '18 at 20:09
  • I thought about it but I inherited after B about 80 different objects, so this way is bad too – Mr Luk Apr 22 '18 at 20:15
  • Btw, I inherited after abstract class only 2 different objects. – Mr Luk Apr 22 '18 at 20:20
  • Why not simply add another entry to the dictionary? I.e. `{ typeof(C),typeof(BView) }`. Or, alternatively, change the dictionary handling so that it checks `if(myObj is B)` instead of `if(typeof(myObj) == typeof(B)`? (this is pseudocode to explain the difference) – Flater Apr 23 '18 at 05:42
  • Because it will be a one-to-many relationship and I need one to one. The second part of the sentence: I can not check, because I have over 100 objects (B, B1, B2, B3 ...) and all have to have link – Mr Luk Apr 23 '18 at 12:38

2 Answers2

4

On any instance of an object you can call .GetType() to get the type of that object.

You don't need to set the type on construction

BlythMeister
  • 299
  • 1
  • 12
2

I didn't understand completely your question, but these are some possibilities to get informations about a type:

var a = new A();
Console.WriteLine(a.GetType().Name); // Output: A
Console.WriteLine(a.GetType().BaseType?.Name); // Output: MyBase

var b = new B();
Console.WriteLine(b.GetType().Name); // Output: B
Console.WriteLine(b.GetType().BaseType?.Name); // Output: A

// A simple loop to get to visit the derivance chain
var currentType = b.GetType();
while (currentType != typeof(object))
{
    Console.WriteLine(currentType.Name);
    currentType = currentType.BaseType;
}
// Output: B A MyBase

Also, I suggest to read this post about the difference between GetType and typeof

Hope this helps.

Francesco Bonizzi
  • 5,142
  • 6
  • 49
  • 88
  • 1
    To extend your answer to what I think is the core of OP's question, notice that `B b = new B()` and `A b = new B()` will yield the same output as is listed for `var b = new B()` in this answer. – Flater Apr 23 '18 at 05:38