Hi guys what is the best practice to pass generic object as parameter, is it possible?I want create my method what can recieve any object and maybe do or not something with it according to conditions inside this method.
public void MyMethod(AnyGenericObject object){
//Anything
}
How can i do it without using inheritance?
What is the “name” of this i am looking for, i found few things on Google but nothing is what i need do you guys have some lead?
Thing is if you use ‘object’ or generics (the ) the compiler doesn’t necessarily know what the object’s type is and therefore you’re limited to accessing it as that generalized object type. If that fits your needs… have at it. But if it doesn’t… well… what is it you need it to do?
Passing by reference to a method or passing by value to a method (depending on what you need). In passing by reference, you pass the reference to the object and when you pass by value, you copy a value of an object to whatever you are passing it to.
If you were to pass something to a method by value:
AMethodToPassAVariableTo(aVariableYouPass);
If you were to pass something to a method by reference:
Thanks for reply , i want create universal save system what i can use in all my project, simply add “SaveSystem” namespace and use static method what will accept as parameter this “generic” object and use it for save somehow, maybe i use binnary formater,json or anything it doesnt metter.
Point is in some project is this save object with 2 variables , sometimes with 20 ,sometimes i have more object for save etc.
So if that’s the case… just like most serializer methods, for example JsonUtility.ToJson:
BinaryFormatter as well:
(note there are security issues with BinaryFormatter… it should be avoided)
JsonConvert.Serialize:
It just takes the object that will be serialized types as “System.Object” (‘object’ for shorthand).
…
Thing is… there are other concerns that may arise when it comes to serializing for a save system is how the serializing engine will support the passed in objects. For example the unity serializer doesn’t really like deserializing polymorphic types (you have a field typed ‘parenttype’ but an instance of ‘childtype’ in it)… that is unless you use SerializeReference attribute (which comes with other complexities on how to use it). And other serializing engines have their own quarks.
Yeah, after reading your question a bit more carefuly i realized that my response might seem a bit deaf. From what i understand now, you want to be able to pass anything that you want to pass to the object and unfortunately i won’t be able to provide a solution on that. My first thought would be to define some sort of a multiclass that contains all the necessary definitions for multiple data types, though again, i don’t know how to do that. But what i’m sure about is: i see that:
you are unsure of what system for saving your data you’re going to use. If your project doesn’t explicitly require being able to use multiple sources of data to read the data from as your savefile, maybe consider picking a format and sticking with it?
That is a reason why i want create some universal save system for all my projects.Currently i am using binnary formatter for file i dont want be edited so easily and for other things like config file what i want edit after build without updating app i use json., but when i create this savesystem i also create some simple gui where i will be able edit these saves after build.For example add another value to list what is stored in that file etc…
Yeah, now i remembered you asking on ways of saving the data so it won’t be easy to edit. Honestly i’d simply make a method for each data type rather than trying to stuff it all together.
If i’m not mistaken, in that thread, @Kurt-Dekker told you that it’s pretty much pointless to try to obfuscate things because as long as the user has direct access to the savefile and enough skill, they’ll add whatever values they want to the files :). I might scare you a bit, but users might also be able to change the values stored in the memory which might kind of make your protected save data useless (again, depending on what kind of project you have). Just my two cents.
I just want to emphasize what lordofduct said. The binary formatter is insecure and considered dangerous by Microsoft (also it’s deprecated). Don’t use it.
Do you even understand what kind of security is meant here? We talk about potential malicious code execution that is not even part of your application. It’s not about “securing” your data. And no, the BinaryFormatter is not really more secure than json. It’s a little bit more obfuscated, but the binary file is actually shipped with your exact class description. So just having your save file I can reconstruct the class with all fields (of course only fields, not methods).
The format used by the binary formatter is well known and well documented and can be found here. I’ve written a parser myself from scratch and there’s also at least one python library out there to parse the remoting format.
Here’s someone who has gone quite into detail explaining the format on an example file. So just as Kurt already said, it’s pretty pointless to put too much efford into obfuscation. There are much easier ways to simply “obfuscate” a json file. Any kind of encryption would work. Heck you could even just xor every byte with a value lie 0x55 and it becomes unreadable. Do it again and it’s back to normal. The more well known formats you use, the easier it is to bypass the obfuscation.
You do know that you are responsible if you are willingly use technology that is known to be a security risk. It’s not your security that is at stake, it’s the security of your users.
I am sorry but I don’t understand how you arrive at this conclusion. Do you have a source for that? I’d be interested. UPDATE: Ah, I assume you meant it’s harder to read by the user. That’s true but that’s not at all the kind of security we meant
Here is the source for BF not being secure: https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide
It also mentions that it is deprecated and will throw an exception in future versions. Therefore getting out of the habit of using it is the more future oriented way. It basically says that BF is like executing an eval() method on a string which is controlled by the user. That’s something that ought to be used with extreme caution (if at all).
It depends on your json format, usually it does not include type information, so you will not have the problem of deserializing the “wrong” type by changing the json.
Note some libs like Newtonsoft JSON actually do support that (to handle abstract types or interfaces) but it’s a known security issue and not recommend to use that specific feature anymore.
This is also the reason why the “new” official System.Text.Json from the .NET Framework does not support it by default, because adding type information to a (de)serializaton system can easily abused.
So to answer your question, yes it’s also possible with JSON (or any other format) if you use a insecure API, but the same way it’s also possible to write a secure binary file if you use the propery API just don’t use the BinaryFormatter (or any other API that requires to add type information to the file)
If your main concern is to make the file “unreadable” you can still use json to serialize your object to string and then make the json text “unreadable” before you save it to the file, for example convert the string to a byte array (with the proper encoding class) then convert the byte array with Convert.ToBase64String(byteArray) and save the result.
It will spit out a string that doesn’t make sense / is unreadable to a normal user and a hacker will find a way to modify the game anyways.
After you load the file you can just do the same in reverse to get back the original string by using Convert.FromBase64String(yourFileContentString) which will return a byte array and then just call the GetString(byteArray) method of your encoding class.
I do not recommend this, because it will also make it harder to debug the file content, but it could be a simple method if you really want to make it unreadable for a normal user.