Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass A Generic (Template) Class For Runtime Initialization

Right now I have a database which initializes a whole bunch of card types as follows:

private void LoadCardTypes()
{
    database.cardTypeList.Clear();
    database.cardTypeMap.Clear();
    Object[] assets = Resources.LoadAll("ScriptableObjects/CardTypes", typeof(Object)) as Object[];
    if (showLog) Debug.Log("Loading CardTypes");
    foreach (Object asset in assets)
    {
        M_CardPlayType type = (M_CardPlayType)asset;

        if (type.name.Length == 0)
        {
            Debug.Log("WARNING: Object Mapped Without Name");
        }

        CheckIdCollision(type, database.cardMap);
        VerifyIdExists(type, CardPlayTypes.SINGLETON);

        database.cardTypeList.Add(type);
        database.cardTypeMap.Add(type.id, type);
    }
}

Where the code goes M_CardPlayType type = (M_CardPlayType)asset; I'd like to make it a template type.

I want it to be something like

private void LoadCardTypes(Type<T> WhateverType)
{
  //other code
  WhateverType type = (WhateverType)asset;
  //other code
  VerifyIdExists(type, WhateverType.SINGLETON);
}

Can this be done? (And bonus question if there is a technique what is it called?).

Update working except 1 part

New signature is

private void LoadNewCardTypes<T,P>(DictionaryOfIntAndSerializableObject map, List<T> list, string path) where T : M_Object where P : ID

My P is giving me trouble. Here is my ID class

public class ID
{
    protected string _className = "ID";
    protected static ID _singleton = new ID();

    public static ID SINGLETON
    {
        get { return _singleton; }
    }
}

When I try to get the singleton from P I get an error (it can't find it)

P.SINGLETON//doesnt work

Do you know why my singleton isn't working in this context?

Final solution thanks to @kailanjian

private void LoadNewCardTypes<T,P>(DictionaryOfIntAndSerializableObject map, List<T> list, string path) where T : M_Object where P : ID
{
    map.Clear();
    list.Clear();
    Object[] assets = Resources.LoadAll(path, typeof(Object)) as Object[];
    if (showLog) Debug.Log("Loading " + path + " types");
    foreach (Object asset in assets)
    {
        T type = (T)asset;

        if (type.name.Length == 0)
        {
            Debug.Log("WARNING: Object Mapped Without Name");
        }

        CheckIdCollision(type, map);
        VerifyIdExists(type, (P)ID.SINGLETON);
    }
like image 515
Aggressor Avatar asked Apr 02 '26 19:04

Aggressor


1 Answers

Try something like this:

private void LoadCardTypes<T>() where T : ParentClass
{
    //other code
    T type = (T)asset;
    //other code
    VerifyIdExists(type, T.SINGLETON);
}

ParentClass is the type that you guarantee is inherited/implemented by any type T you pass. It should have the SINGLETON value as one of its values for you to be able to use it.

Source: MSDN Generic Methods

like image 157
Krikor Ailanjian Avatar answered Apr 04 '26 08:04

Krikor Ailanjian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!