1

My question is simple, I have a Type input in the code below and I want to cast it to the Core class and get the Info variable which is static. How can I access the static variable via type input?

private static bool Button(Type type)
{
    if (type is Core core) 
    {
        Debug.Log(core.Info); // Info is a static string in Core class...
    }
}

Now I have the following error:

enter image description here

For Better Understanding This Question, Here I have a mother core class that gives the inherited classes as type to the Button input. Now it should read the static variable of my inputs.

public abstract class Core
{
    public abstract string Info { get; }
}
public class CoreA : Core
{
    public override string Info => "Core A Info";
}

public class CoreB : Core
{
    public override string Info => "Core B Info";
}
public class SomeClass
{
    private void MainStart()
    {
        Panel.Button(typeof(CoreB)); // type: "Core B Info"
    }
}
KiynL
  • 4,097
  • 2
  • 16
  • 34
  • Do you mean `if (type == typeof(Core)) Debug.Log(Core.Info);`? – Sweeper Jun 25 '22 at 13:29
  • @Sweeper No, the core must be taken from the type. (Core = (type From RaycastCore)).Info – KiynL Jun 25 '22 at 13:30
  • What do you mean by "the core must be taken from the type"? What does "(Core = (type From RaycastCore)).Info" mean? That is not C# syntax. – Sweeper Jun 25 '22 at 13:33
  • @Sweeper (Top wasn't syntax, It was for better meaning) The core must be equal as input type. Each different type has its own static variable. Here we have to convert the type to core (as temp variable to access Info). I actually want to get the static of specific type entered in parameters. – KiynL Jun 25 '22 at 13:40
  • For examle: Type entered DogCore : debug.Log // Dogs are loyal creatures. Type entered CatCore : debug.Log // Cats are very neat creatures. – KiynL Jun 25 '22 at 13:42
  • So you are trying to dispatch a static property dynamically... Well that's just the opposite of "static". [You can do it with reflection though, if you don't mind using reflection.](https://stackoverflow.com/a/3386869/5133585) – Sweeper Jun 25 '22 at 13:44
  • 1
    In your edit, none of the properties are actually static? Just when I thought I understood your problem, I got more confused by your edit. – Sweeper Jun 25 '22 at 13:50
  • @Sweeper It is not really static, but it acts like a singleton and is the same for all instances. It would be great if they could be static, but my current problem is calling Info via input type. – KiynL Jun 25 '22 at 13:58
  • I am pretty new to C#, but I am wondering why the parameter to `Button` has type `Type`. I was expecting `Core`. Am I really that lost in C# or can you create a better (complete) example? – Luuk Jun 25 '22 at 13:58
  • 1
    @KiynL you are conflating two parallel concepts that don't work together in the way you are intuiting. Reflection is a metadata subsystem (of which `System.Type` is a participant) and is entirely orthogonal to actual types in C# (i.e. `x is MyClass`). – Kirk Woll Jun 25 '22 at 14:01

0 Answers0