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

Deepak RawatRank #646 · 3.7K reputationPosted Jun 18, 2023, 5:39 AM EDT
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:
using System; using System.IO; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; namespace DllLoader { public class ClassLoader { public T DeserializeFromDll(string dllPath) { Assembly assembly = Assembly.LoadFrom(dllPath); // Assuming the class you want to deserialize is named "Me" Type targetType = assembly.GetType("WindowsFormsApp15.Me"); if (targetType == null) { throw new Exception("Target type not found in the DLL."); } // Create an instance of BinaryFormatter to deserialize the object BinaryFormatter formatter = new BinaryFormatter(); // Load the DLL file into memory byte[] fileData = File.ReadAllBytes(dllPath); // Create a MemoryStream from the DLL data using (MemoryStream memoryStream = new MemoryStream(fileData)) { // Deserialize the object from the MemoryStream object deserializedObject = formatter.Deserialize(memoryStream); if (deserializedObject is T result) { return result; } else { throw new Exception("Deserialized object type does not match the requested type."); } } } } }ClassLoaderclass to deserialize the class from a DLL dynamically. Here's an example:var dllPath = "path/to/joseph.dll"; // Provide the path to the DLL file ClassLoader classLoader = new ClassLoader(); var deserializedObject = classLoader.DeserializeFromDll(dllPath); // Access the deserialized object's properties string sarah = deserializedObject.sarah; int number = deserializedObject.number; // Use the properties as needed MessageBox.Show(sarah);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 joseph115 reputationPosted Jun 9, 2023, 9:21 AM EDT
thanks but there is exception! System.Runtime.Serialization.SerializationException
HResult=0x8013150C
Message=Attempting to deserialize an empty stream.
Source=
mscorlib
Tuhin PaulRank #7 · 71.9K reputationPosted Jun 18, 2023, 11:26 AM EDT
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.
Tuhin PaulRank #7 · 71.9K reputationPosted Jun 18, 2023, 11:27 AM EDT
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.
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted Jun 9, 2023, 2:32 AM EDT
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:
using System; [Serializable] public class Me { public string sarah = "joseph"; public int number = 110; // more fields }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:
using System; using System.IO; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; class Program { static void Main(string[] args) { // Specify the path to the DLL file string dllPath = "path/to/joseph.dll"; // Load the assembly dynamically Assembly assembly = Assembly.LoadFrom(dllPath); // Get the type of the class you want to deserialize Type classType = assembly.GetType("WindowsFormsApp15.Me"); // Create an instance of the class object instance = Activator.CreateInstance(classType); // Deserialize the instance BinaryFormatter formatter = new BinaryFormatter(); using (Stream stream = new MemoryStream()) { // Deserialize the object from the DLL formatter.Deserialize(stream); // Cast the deserialized object to the appropriate type Me deserializedObject = (Me)instance; // Access the fields of the deserialized object Console.WriteLine(deserializedObject.sarah); Console.WriteLine(deserializedObject.number); } } }