Get Object of unknown type with unknown parameters from string
I have an abstract parent class with several child classes, each child class has an export method that returns a string that hold the data of the instance of the subclass in the form of:
[SUBCLASS_NAME] | [PARAM#1] | [PARAM#2] | [PARAM#3]...
and so on for all of the parameters, these parameters can be Enum values (The Enums are located in the parent class), strings, integers, and possibly in the future, doubles / floats.
I want to write a static method in the parent class that will return an instance of the child class defined at the beginning of the input string, that will then use the remaining data in the string as parameters.
Currently I lack the understanding of types as well as the GetConstructor method, and also the understanding to assemble the type list properly, I've read the official documentation but it was a little too complex to grasp, could somebody help me figure out how to achieve this?
Attached is my current attempt.
24 Replies
tList.Add(…)
you just need o.GetType()
there
you don't need the tryparse loop either
ah right you have types as strings in slist
you need to Type.GetType($"{parentClass}.{sList[i]")
also they might be (and likely will be) in some namespace
how do you want to handle that?well, they're all in the same namespace as the parent class, so I didn't think that would be an issue
I mean the parent type
oh... I didn't know factoring in the namespace was an issue
does subclass name include the namespace?
I don't think it will be able to find the type otherwise
If they're not public, I think you need to pass binding flags
binding flags?
an optional parameter
ah no
that's for properties
alright
I'll update my code and get back to you to see if it works, thank you so much
yw
I'm still not fully sure I understand
my intent is to be able to take:
and plug it into the constructor
and have it return the EftDamage object with parameters filled in, while also having the same code be able to handle taking
and plug it into
an return the EftDuration object with its parameters filled in.
All classes are in the same namespace and are children classes of the ActEffect class I'm writing this method in.
I'n not sure if my current code is on the right path or not.
(it may be worth noting that both the Enums TickType and TargetType are in the class that this is being written in)
I see
why is that a string again?
well, I suppose it doesnt have to be, String was the immediate data type I say that could represent all the other data types I'd need, and as an added bonus I can use it in the BinaryWriter to easily write these onjects to files (these are only going to be a small aprt of the actual file that is exported)
what do you need this for?
If it's for scripting or configuration, json might be better
well, I would specifically like to use the BinaryReader and BinaryWriter if possible, I want to make a custom file type when exporting and importing them
its for a video game
If it's going to be a custom format and you're going to remake this later, don't overcomplicate the format right now. Use json or xml.
if you want a custo format like you wrote you need a couple of things
you should write a parser
and not cut any corners when it comes to parsing
you want to type check both the parent type and the argument types in advance
you probably want a registry for your effect types, so that the user can't instantiate just any type this way
for security reasons
Type.GetType should generally never be used
if you don't have a registry, you need to actually store the metadata name in the string
registry?
does this count?
sorry
wrong ss
this here is where I make a list of all the subclasses, of this class. It works
yeah, search in this list when you look for the type
not Type.GetType
alright, and should I just manually put in an array of types to each effect type that I call when interpreting the input string?
for the parameters.
or is there a more automatic way to get the type list from a constructor?
make a class for a registry with this info
pass that as a parameter to your method that does the instantiation
you don't need it in the constructor
you need it in the method that parses the serialized data
alright, I'll do my best. Thank you for your help and time