How do I cast with GetType() or Type variables without reflection?

Asked 1 years ago, Updated 1 years ago, 34 views

I'm making a game with Unity's C#.
A 64-bit C++ build called IL2CPP has led to no reflection.


without reflection when creating API communications
in a unique class that inherits the response class on which API received data is based. I had to cast it, and I couldn't do it, so I chose the test class
I was going to make it, but I don't know how to do it, so I'm worried.

There are the following classes.
·Basic Class A
·Class B and C
that inherited A ·JsonTestManager test class to serialize and de-serialize LitJson ·OnDebug calling class

When OnClickButton in OnDebug runs
Data is created in ClassB and ClassC and
I purposely put the data in a ClassA buffer in JsonTestManager.

I would like to specify the type of inheritance destination in the following part, but I don't know what to do.
With Unity, it's up to C#3.0, so Dynamic is not available, and reflection is no longer available.
When the response type of the inheritance destination has changed, it cannot be dealt with.

testClassCC=LitJson.JsonMapper.ToObject<Inherited Type>(str);

■ ClassA

using UnityEngine;
using System.Collections;

public class ClassA
{
    public int aaa;
    public int bbb;
}

■ ClassB

using UnityEngine;
using System.Collections;

public class ClassB —ClassA
{
    public string test1;
    public string test2;
}

■ ClassC

using UnityEngine;
using System.Collections;

public class ClassC —ClassA
{
    public string test3;
    public string test4;
}

■JsonTestManager

using UnityEngine;
using System.Text;
using System.Collections;
usingLitJson;

public class JsonTestManager
{
    publicClassAtestClassB;
    public ClassA testClassC;
    public ClassA testClassCC;

    public void JsonTest()
    {
        // Convert data to JsonString for transmission
        string litjsonString=LitJson.JsonMapper.ToJson(testClassC);

        byte[] postBytes=Encoding.Default.GetBytes(litjsonString);

        string str = Encoding.UTF8.GetString(postBytes);

        testClassCC = LitJson.JsonMapper.ToObject<Inherited Type>(str);
    }
}

■ Calling Class OnDebug -- Tap the button to call OnClickButton().

using UnityEngine;
using System.Collections;

public class OnDebug
{
    public JsonTestManager jsonTestManager = new JsonTestManager();

    public void OnClickButton()
    {
        ClassB classB = new ClassB();
        classB.test1 = "test1";
        classB.test2 = "test2";
        ClassC classC = new ClassC();
        classC.test3 = "test3";
        classC.test4 = "test4";

        jsonTestManager.testClassB=classB;
        jsonTestManager.testClassC=classC;
        jsonTestManager.testClassCC=classC;

        jsonTestManager.JsonTest();
    }
}

c# unity3d

2022-09-30 14:06

1 Answers

The generic class generic method type parameters must be determined at compile time, so you cannot dynamically specify a type, such as a Type, unless you use reflection.

However, if you don't need to change the type at runtime, and you want to be able to decide the type from the person who uses JsonTestManager, you can solve it by making it generic for each JsonTestManager.

If you rewrite the code written in the question using generic, would this be what it looks like?

public class JsonTestManager<T1,T2,T3>
{
    public T1 testClassB;
    public T2testClassC;
    public T3testClassCC;

    public void JsonTest()
    {
        string litjsonString=LitJson.JsonMapper.ToJson(testClassC);
        // Shouldn't it be unified into either Default or UTF8?
        byte[] postBytes=Encoding.Default.GetBytes(litjsonString);
        string str = Encoding.UTF8.GetString(postBytes);
        testClassCC = LitJson.JsonMapper.ToObject<T3>(str);
    }
}

public class OnDebug
{
    public void OnClickButton()
    {
        ClassB classB = new ClassB();
        classB.test1 = "test1";
        classB.test2 = "test2";
        ClassC classC = new ClassC();
        classC.test3 = "test3";
        classC.test4 = "test4";

        varjsonTestManager = new JsonTestManager <ClassB, ClassC, ClassC>();

        jsonTestManager.testClassB=classB;
        jsonTestManager.testClassC=classC;

        jsonTestManager.JsonTest();
    }
}

If it's just "convert testClassB to JSON and return it back to the object" (I deleted it because it doesn't seem to be approximately .

Alternatively, if ToObject<T>() is all you need to do, you can leave the field ClassA and pass the type parameter to ToObject<T>() to JsonTest().This is easier if you want to use singleton or use instances around.

public class JsonTestManager
{
    publicClassAtestClassB;
    public ClassA testClassC;
    public ClassA testClassCC;

    public void JsonTest<Tresult>()
    {
        string litjsonString=LitJson.JsonMapper.ToJson(testClassC);
        byte[] postBytes=Encoding.Default.GetBytes(litjsonString);
        string str = Encoding.UTF8.GetString(postBytes);
        testClassCC = LitJson.JsonMapper.ToObject<Tresult>(str);
    }
}

public class OnDebug
{
    public JsonTestManager jsonTestManager = new JsonTestManager();

    public void OnClickButton()
    {
        ClassB classB = new ClassB();
        classB.test1 = "test1";
        classB.test2 = "test2";
        ClassC classC = new ClassC();
        classC.test3 = "test3";
        classC.test4 = "test4";

        jsonTestManager.testClassB=classB;
        jsonTestManager.testClassC=classC;

        jsonTestManager.JsonTest<ClassC>();

        // Field type is ClassA, so cast is required to treat it as a derivative
        varclassCC=(ClassC)jsonTestManager.testClassCC;
    }
}


2022-09-30 14:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.