samedi 20 août 2016

Using System.type as Type paramater

I'm trying to create a database that has a generic use for everything table filler based on the object passed. This object passed in is a reference object with just public variables and sub classes that are filled by a inheritance export on my main Object class. The DB tabled are created and updated a similar way to how i want to fill the tables but each class is hard coded into the method. At the moment i have this:

#region insert / object Creation

    private static void AddObject(ObjectReference or)
    {
        Dictionary<string, System.Type> classes = GetSubClassList<ObjectReference>();

        foreach(KeyValuePair<string, System.Type> kv in classes)
        {
            AddOrUpdateObjectToDB<kv.Value>(or);
        }
    }

    private static void AddOrUpdateObjectToDB<T>(ObjectReference or)
    {
        Dictionary<string, System.Type> properties = GetPropertyList<T>();

        foreach(KeyValuePair<string, System.Type> kv in properties)
        {
            Debug.LogWarning(kv.Key); // Warning for visability
        }
    }

    #endregion

But "AddOrUpdateObjectToDB<kv.Value>(or);" is not allowed.

GetSubClassList is using reflection to get the system.types of all public sub classes. I want to use this type to pass into AddOrUpdateObjectToDB, where reflection then gets the properties (Properties are the same name in the DB) for that class for the object reference to then use to add to the DB. This is so I don't have to keep adding case statements for each sub class that may exist to pass into the AddOrUpdate. Is this possible?

Example of ObjectReference if needed:

public class ObjectReference
{
    public string DatabaseID;
    public bool HoverDetails;
    public bool ReceivesWind;
    public string ItemModel;

    public Transform transform;

    //
    [System.Serializable]
    public class Transform
    {
        public Vector3 Scale;
    }
}





Aucun commentaire:

Enregistrer un commentaire