@manhnguyenv

Welcome to my blog!

Custom Model Binding

May 7, 2018 23:00

Custom Model Binding (edit)

https://malvinly.com/2011/06/22/custom-model-binding/

Html/Razor Helper (HAY)

https://malvinly.com/2011/02/28/razor-declarative-helpers/

@Demo.SampleHelper("hello world")

Archiving Emails (HAY)

https://malvinly.com/2014/01/22/archiving-emails-the-hard-way/

https://malvinly.com/2014/01/23/archiving-emails-part-ii/

Encrypt/Decrypt Files (HAY)

https://malvinly.com/2014/10/16/encrypting-files/

Loading types at runtime (HAY)

https://malvinly.com/2011/09/23/loading-types-at-runtime/

public interface IPrinter
{
    bool CanHandle(string color);
    void Print(string message);
}
public class PrinterFactory
{
    private static IList<IPrinter> Printers { get; set; }
 
    static PrinterFactory()
    {
        var catalog = new DirectoryCatalog(Environment.CurrentDirectory, "Demo.*");
        var container = new CompositionContainer(catalog);
 
        Printers = container.GetExportedValues<IPrinter>().ToList();
    }
         
    public IPrinter Create(string color)
    {
        foreach (IPrinter printer in Printers)
        {
            if (printer.CanHandle(color))
                return printer;
        }
 
        return null;
    }
}
static void Main(string[] args)
{
    var factory = new PrinterFactory();
    RedPrinter printer = factory.Create("red") as RedPrinter;
 
    printer.Print("hello world");
}
static PrinterFactory()
{
    string[] paths = Directory.GetFiles(Environment.CurrentDirectory, "Demo.*.dll");
 
    foreach (string path in paths)
        Assembly.LoadFrom(path);
 
    Printers = AppDomain.CurrentDomain
        .GetAssemblies()
        .SelectMany(x => x.GetTypes())
        .Where(x => typeof(IPrinter).IsAssignableFrom(x) && !x.IsAbstract && !x.IsInterface)
        .Select(x => (IPrinter)Activator.CreateInstance(x))
        .ToList();
}

https://malvinly.com/2016/11/16/check-whether-a-net-dll-is-built-for-any-cpu-x86-or-x64/

.NET Framework SDK: CorFlags.exe 

CorFlags "C:\example.dll"

Microsoft (R) .NET Framework CorFlags Conversion Tool.  Version  4.6.81.0

Copyright (c) Microsoft Corporation.  All rights reserved.
 
Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32
CorFlags  : 0x3
ILONLY    : 1
32BITREQ  : 1
32BITPREF : 0
Signed    : 0
Assembly a = Assembly.ReflectionOnlyLoadFrom(@"C:\example.dll");
 
PortableExecutableKinds peKind;
ImageFileMachine machine;
 
a.ManifestModule.GetPEKind(out peKind, out machine);
 
Console.WriteLine(peKind);

Categories