Is there any way to return parameter type instead of a parameter value from a function?
List<DetermineCorrectType(0)> myTypeList = new List<DetermineCorrectType(0)>;
public Type DetermineCorrectType(givenVersion) {
if (givenVersion == 0) {
return saveVersion1;
} else if (givenVersion == 8) {
return saveVersion2;
} else {
return saveVersion4;
}
}
I have a bunch of serializable classes that is use for backwards compatibility with previous saves (when the user updates version). I was hoping to write a function that would return the correct class for the given version.
Itâs confusing UnityEngine.Object and the .NETâs object, If youâre not using Monobehavior (GameObject script) then just exclude âusing unityEngineâ stuff from the top of your code.
Otherwise, refer to it as âSystem.Objectâ instead of just Object. I corrected it using System.Object in the quotes.
That works great, up until the bit where I try to create a list of that type. I get âUsing the generic type âListâ requires 1 type argumentsâ. I am really lost here.
I realize now that I need to deserialize into that list. But it rejects this method.
//In this instance listType returns 'saveVersion.saveVersion2' type, or rather the monobehavior 'saveVersion+saveVersion2'
var listType = typeof(List<>).MakeGenericType(DetermineCorrectType(0).GetType());
CSharp]savedGames = (List<listType>)bf.Deserialize(file);
//Returns "Cannot implicitly convert type 'System.Collections.Generic.List<listType>' to 'System.Collectoins.Generic.List<saveVersion.saveVersion2>'
//I have tried various ways of using the 'System.Activator' line in this, but I frankly don't understand it enough to put an example up that is remotely close to what might work
Is there any way to get a function to return a type where I can deserialize a file into a list of that type? (final addition, I promise!)
Yeah, You canât actually do this. What you are trying to do, is essentially cast an object to a different type at runtime, while actually a cast operation happens at compile time. Judging by the response from your compiler, it should be something like :
The question is where do you define CSharpSavedGames, since this is what you are trying to cast to. Also, what is Serialize() returning?
I went down this path with a serializer I was writing and ran into these problems you are facing in the past. It never actually worked out properly, and just created more issues. I would suggest that you re-consider your approach.