hello, i want to make deserialization of class from dll in runtime regarding to ability to change the dll path.
i clearify more
firstly, why i don't use xml or json?
1 because dll is more fast than others specially when the class has more and more fields.
2 i don't want to make the file to be readable to the user
now, i write what i made and i ask for help
1 i make serialized class such as
namespace WindowsFormsApp15
}
[Serializable]
public class Me
{
public string sarah = "sarah";
public int number = 55;
//more and more. etc
}
}
2 i save this class as dll file. with name "sarah.dll"
3 i create other class with the same fields but with other values such as
namespace WindowsFormsApp15
}
[Serializable]
public class Me
{
public string sarah = "joseph";
public int number = 110;
//more and more. etc
}
}
4 i save this class as dll file. with name "joseph.dll";
5 i imported the first dll (sarah.dll)to my project as reference
6 i use "use WindowsFormsApp15"
7 i make object from this class such as
public WindowsFormsApp15.me sarsoura= new WindowsFormsApp15.me = ();
8 i want to show message with string "sarah"
MessageBox.Show(sarsoura.sarah);
now everything is ok
but i want to deserialized class from the second dll (joseph.dll) as runtime.
my idea that, i make ability to users to create more and more dlls from significant class in runtime as they want.
and call the dlls again from its path to fetch data from them
how i can achieve that?
i use more ways but unfortunally without any success
please if anyone want to help me, please give me example or project as attachment with solution.
Loading
Tuhin PaulPosted Jun 18, 2023, 3:27 PM
Reflection and BinaryFormatter can have performance implications, especially when dealing with large or complex objects. @Deepak Rawat - Deserialization using BinaryFormatter involves translating binary data into object instances, which can be slower compared to other serialization techniques like JSON or XML.
Tuhin PaulPosted Jun 18, 2023, 3:26 PM
If the DLL you're loading dynamically changes its structure or dependencies between different versions, it may lead to compatibility issues. That means if the deserialization code relies on specific properties or methods that are no longer present in the new version of the DLL, it can cause errors or unexpected behavior.
Deepak RawatPosted Jun 18, 2023, 9:39 AM
To dynamically deserialize a class from a DLL at runtime, you can use reflection in C#. Here's an example of how you can achieve this:
Create a separate project (e.g., "DllLoader") in your solution to handle the dynamic loading and deserialization of DLLs.
In the DllLoader project, add a reference to the DLLs you want to load dynamically. Make sure the DLLs are located in a directory that is accessible to your application.
Use reflection to load the DLL at runtime and deserialize the class from it. Here's a sample code snippet:
ClassLoaderclass to deserialize the class from a DLL dynamically. Here's an example:Remember to replace "path/to/joseph.dll" with the actual path to the DLL file you want to deserialize.
This approach uses the
BinaryFormatterfor deserialization. Note that using theBinaryFormatterhas security implications, so ensure that you trust the source of the DLL files you are loading dynamically.I hope this helps you achieve your goal of dynamically deserializing classes from DLLs at runtime.
sarah josephPosted Jun 9, 2023, 1:21 PM
thanks but there is exception! System.Runtime.Serialization.SerializationException
HResult=0x8013150C
Message=Attempting to deserialize an empty stream.
Source=
mscorlib
Mohamed Azarudeen ZPosted Jun 9, 2023, 6:32 AM
Create a new Console Application project in Visual Studio.
Create a separate Class Library project within the solution. Let's call it "MyClasses".
In the "MyClasses" project, define your serializable class with different values. For example:
Build the "MyClasses" project to generate the DLL file.
In the Console Application project, add a reference to the "MyClasses" DLL (right-click References > Add Reference > Browse).
In the Console Application's code, you can dynamically load the DLL file at runtime and deserialize the class. Here's an example: