Показаны сообщения с ярлыком Serialization. Показать все сообщения
Показаны сообщения с ярлыком Serialization. Показать все сообщения

понедельник, 14 октября 2019 г.

Serialization/DeSerialization List

https://stackoverflow.com/questions/608110/is-it-possible-to-deserialize-xml-into-listt

https://docs.microsoft.com/ru-ru/dotnet/standard/serialization/controlling-xml-serialization-using-attributes

private void Serialize<T>(XDocument doc, List<T> paramList)
        {
            var serializer = new System.Xml.Serialization.XmlSerializer(paramList.GetType());
            var writer = doc.CreateWriter();
            serializer.Serialize(writer, paramList);
            writer.Close();
        }

        private List<T> Deserialize<T>(XDocument doc)
        {
            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<T>));
            var reader = doc.CreateReader();
            var result = (List<T>)serializer.Deserialize(reader);
            reader.Close();
            return result;
        }

https://docs.microsoft.com/ru-ru/dotnet/standard/serialization/examples-of-xml-serialization

воскресенье, 14 января 2018 г.

File To Bas64String Convert

BinaryWriter, BinaryReader

Writer

https://msdn.microsoft.com/ru-ru/library/system.io.binarywriter(v=vs.110).aspx

https://msdn.microsoft.com/ru-ru/library/system.io.binarywriter.write(v=vs.110).aspx

https://msdn.microsoft.com/ru-ru/library/yzxa6408(v=vs.110).aspx

https://metanit.com/sharp/tutorial/5.6.php

https://www.dotnetperls.com/binarywriter

https://stackoverflow.com/questions/4614318/whats-the-difference-between-a-streamwriter-and-a-binarywriter

http://csharp.net-informations.com/file/csharp-binarywriter.htm

https://professorweb.ru/my/csharp/thread_and_files/level3/3_11.php

Reader

https://msdn.microsoft.com/ru-ru/library/system.io.binaryreader(v=vs.110).aspx

https://metanit.com/sharp/tutorial/5.6.php

https://professorweb.ru/my/csharp/thread_and_files/level3/3_11.php

https://www.dotnetperls.com/binaryreader

https://ru.stackoverflow.com/questions/355195/binaryreader-%D0%9A%D0%B0%D0%BA-%D0%BE%D0%BF%D1%80%D0%B5%D0%B4%D0%B5%D0%BB%D0%B8%D1%82%D1%8C-%D0%BA%D0%BE%D0%BD%D0%B5%D1%86-%D1%84%D0%B0%D0%B9%D0%BB%D0%B0

http://csharp.net-informations.com/file/csharp-binaryreader.htm



Binary Serialization

https://docs.microsoft.com/en-us/dotnet/standard/serialization/binary-serialization

https://msdn.microsoft.com/ru-ru/library/system.runtime.serialization.formatters.binary.binaryformatter(v=vs.110).aspx

https://www.centerspace.net/examples/nmath/csharp/core/binary-serialization-example.php

https://msdn.microsoft.com/ru-ru/library/c5sbs8z9(v=vs.110).aspx

https://docs.microsoft.com/en-us/dotnet/standard/serialization/binary-serialization

https://metanit.com/sharp/tutorial/6.2.php

https://gist.github.com/vaclavbohac/962739

https://stackoverflow.com/questions/1749044/c-sharp-object-binary-serialization

http://www.ezzylearning.com/tutorial/binary-serialization-and-deserialization-in-csharp

https://johnlnelson.com/2014/07/01/binary-serialization-with-c-and-net/

https://www.codeproject.com/Articles/254617/Serialization-Part-I-Binary-Serialization


воскресенье, 31 декабря 2017 г.

Assembly Loading

https://stackoverflow.com/questions/658498/how-to-load-an-assembly-to-appdomain-with-all-references-recursively

Once you pass the assembly instance back to the caller domain, the caller domain will try to load it! This is why you get the exception. This happens in your last line of code:

domain.Load(AssemblyName.GetAssemblyName(path));
Thus, whatever you want to do with the assembly, should be done in a proxy class - a class which inherit MarshalByRefObject.
Take in count that the caller domain and the new created domain should both have access to the proxy class assembly. If your issue is not too complicated, consider leaving the ApplicationBase folder unchanged, so it will be same as the caller domain folder (the new domain will only load Assemblies it needs).
public void DoStuffInOtherDomain()
{
    const string assemblyPath = @"[AsmPath]";
    var newDomain = AppDomain.CreateDomain("newDomain");
    var asmLoaderProxy = (ProxyDomain)newDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(ProxyDomain).FullName);

    asmLoaderProxy.GetAssembly(assemblyPath);
}

class ProxyDomain : MarshalByRefObject
{
    public void GetAssembly(string AssemblyPath)
    {
        try
        {
            Assembly.LoadFrom(AssemblyPath);
            //If you want to do anything further to that assembly, you need to do it here.
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException(ex.Message, ex);
        }
    }
}
If you do need to load the assemblies from a folder which is different than you current app domain folder, create the new app domain with specific dlls search path folder.
For example, the app domain creation line from the above code should be replaced with:
var dllsSearchPath = @"[dlls search path for new app domain]";
AppDomain newDomain = AppDomain.CreateDomain("newDomain", new Evidence(), dllsSearchPath, "", true);
This way, all the dlls will automaically be resolved from dllsSearchPath.
[STAThread]
static void Main(string[] args)
{
    fileDialog.ShowDialog();
    string fileName = fileDialog.FileName;
    if (string.IsNullOrEmpty(fileName) == false)
    {
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        if (Directory.Exists(@"c:\Provisioning\") == false)
            Directory.CreateDirectory(@"c:\Provisioning\");

        assemblyDirectory = Path.GetDirectoryName(fileName);
        Assembly loadedAssembly = Assembly.LoadFile(fileName);

        List<Type> assemblyTypes = loadedAssembly.GetTypes().ToList<Type>();

        foreach (var type in assemblyTypes)
        {
            if (type.IsInterface == false)
            {
                StreamWriter jsonFile = File.CreateText(string.Format(@"c:\Provisioning\{0}.json", type.Name));
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                jsonFile.WriteLine(serializer.Serialize(Activator.CreateInstance(type)));
                jsonFile.Close();
            }
        }
    }
}

static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    string[] tokens = args.Name.Split(",".ToCharArray());
    System.Diagnostics.Debug.WriteLine("Resolving : " + args.Name);
    return Assembly.LoadFile(Path.Combine(new string[]{assemblyDirectory,tokens[0]+ ".dll"}));
}