Storing types as strings
I need to store a Type as a string, then convert it back to a type. How do i do this? I've tried
Type.Name
then doing Type.GetType(typeName)
but this returns null17 Replies
You need the full type name
Type.FullName
@hutonahill what for? Just out of curiosity.
I working on a system for saving configruable settings in a json. I need to save the datatype so i can pasre the user input into the right type for the new setting value
So parsing that to and from JSON
This might lead to some security risks 😅
very likely
not doing anything super secure with it though
this is the settings for a personal discord bot.
what risks do you see though?
Well it depends on how you're loading the data back, because if you're creating an instance of any
type
you read then it's probably not securenot sure what you mean... an instance of SettingBase?
No the actual
type
you're reading from the JSON file
How are you loading the data from the JSON file?How to serialize properties of derived classes with System.Text.Jso...
Learn how to serialize polymorphic objects while serializing to and deserializing from JSON in .NET.
i made an implimentation of
JsonConverter
I don't personally see any security problems here, but I'm no security expert
But you're probably better off following what jcotton linked, although it's for STJ not Newtonsoft
Which is additional reason to go with it
All that said, by the by, I find it interesting that configuration/settings would have no enforced shape
Kinda hard to reason about it in the code, if the config file can be "whatever lmao"
no that's the great part. i can store values in any shape, i do have a data type that stores all these values, my config file has a
List<SettingBase>
and my bot allows you to manage all the values in that list. That's the whole point of this data structure: a settings file you can easily add and configure in the command line.But what consumes those settings?
on start up i load them into an object and methods read the object
then ive got a method to update the object and save its current state when i run the methods that let me modify the settings
is taht what you mean?
Newtonsoft.Json :wires:
looked into and implimented