限制类上的通用方法的类型

huangapple go评论71阅读模式
英文:

Restrict the type of a generic method on a class

问题

以下是翻译好的部分:

"I am currently working on a save system that will allow me to save data in Unity. I have a problem when it comes to the generic part. When the program wants to load a new element of type T, he will check if it exists then load it. If the element doesn't exist, a new element will be created using a constructor in the class T.

A problem happens when I tried to name the new element. I created a new class named Savable which will be parent for every data I will need to save, but I don't know how to restrict the type of the generic method so that T is considered as an element of type Savable, but the return object is of type T.

I currently tried to use the class T and to use a constructor with parameter, but it doesn't work.

Here is my current code."

"我目前正在开发一个保存系统,允许我在Unity中保存数据。在泛型部分出现了问题。当程序想要加载一个类型为T的新元素时,它会检查是否存在,然后加载它。如果元素不存在,将使用类T中的构造函数创建一个新元素。

当我尝试为新元素命名时出现问题。我创建了一个名为Savable的新类,它将成为我需要保存的每个数据的父类,但我不知道如何限制泛型方法的类型,以便T被视为Savable类型的元素,但返回对象的类型为T。

我目前尝试使用类T和带参数的构造函数,但它不起作用。

这是我的当前代码。"

"PS:对于我的英语,我很抱歉,这是我的第二语言。"

"The solution was found. I just needed to add the constraint Savable before the constraint new()."

"解决方案已找到。我只需要在new()约束之前添加Savable约束。"

英文:

I am currently working on a save system that will allow me to save data in Unity. I have a problem when it comes to the generic part. When the program wants to load a new element of type T, he will check if it exists then load it. If the element doesn't exist, a new element will be created using a constructor in the class T.

A problem happens when I tried to name the new element. I created a new class named Savable which will be parent for every data I will need to save, but I don't know how to restrict the type of the generic method so that T is considered as an element of type Savable, but the return object is of type T.

I currently tried to use the class T and to use a constructor with parameter, but it doesn't work.

Here is my current code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public static class SaveManager
{
    public static void SaveFromJson<T>(T data, string name, string path)
    {

        path = Application.persistentDataPath + path;

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        path = path + name + ".txt";

        string json = JsonUtility.ToJson(data, true);

        File.WriteAllText(path, json);
    }

    public static T LoadFromJson<T>(string name, string path) where T : new()
    {
        path = Application.persistentDataPath + path + name + ".txt";

        Savable data = new T();

        if (Directory.Exists(path))
        {
            string json = File.ReadAllText(path);

            data = JsonUtility.FromJson<T>(json);
        }
        else
        {
            data.Name = name;
        }

        return data;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Savable
{
    public string Name;
}

PS : Sorry for my English, it is my second language.

The solution was found. I just needed to add the constraint Savable before the constraint new().

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public static class SaveManager
{
    public static void SaveFromJson<T>(T data, string name, string path)
    {

        path = Application.persistentDataPath + path;

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        path = path + name + ".txt";

        string json = JsonUtility.ToJson(data, true);

        File.WriteAllText(path, json);
    }

    public static T LoadFromJson<T>(string name, string path) where T : Savable, new()
    {
        path = Application.persistentDataPath + path + name + ".txt";

        T data = new T();

        if (Directory.Exists(path))
        {
            string json = File.ReadAllText(path);

            data = JsonUtility.FromJson<T>(json);
        }
        else
        {
            data.Name = name;
        }

        return data;
    }
}

答案1

得分: 0

解决方案已找到。我只需要在 new() 约束前添加 Savable 约束。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public static class SaveManager
{
    public static void SaveFromJson<T>(T data, string name, string path)
    {

        path = Application.persistentDataPath + path;

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        path = path + name + ".txt";

        string json = JsonUtility.ToJson(data, true);

        File.WriteAllText(path, json);
    }

    public static T LoadFromJson<T>(string name, string path) where T : Savable, new()
    {
        path = Application.persistentDataPath + path + name + ".txt";

        T data = new T();

        if (Directory.Exists(path))
        {
            string json = File.ReadAllText(path);

            data = JsonUtility.FromJson<T>(json);
        }
        else
        {
            data.Name = name;
        }

        return data;
    }
}

如果您需要进一步的帮助,请随时告诉我。

英文:

The solution was found. I just needed to add the constraint Savable before the constraint new().

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public static class SaveManager
{
    public static void SaveFromJson&lt;T&gt;(T data, string name, string path)
    {

        path = Application.persistentDataPath + path;

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        path = path + name + &quot;.txt&quot;;

        string json = JsonUtility.ToJson(data, true);

        File.WriteAllText(path, json);
    }

    public static T LoadFromJson&lt;T&gt;(string name, string path) where T : Savable, new()
    {
        path = Application.persistentDataPath + path + name + &quot;.txt&quot;;

        T data = new T();

        if (Directory.Exists(path))
        {
            string json = File.ReadAllText(path);

            data = JsonUtility.FromJson&lt;T&gt;(json);
        }
        else
        {
            data.Name = name;
        }

        return data;
    }
}

huangapple
  • 本文由 发表于 2023年6月5日 11:24:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403327.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定