lundi 17 octobre 2016

Getting Struct from DLL with C# reflection

I want to access a DLL which supports several different structs with different members, for example

typedef struct PanTilt
    {
    float PTCorrectionGradPan0;
    float PTCorrectionGradPan90;
    }   PanTiltValues;

typedef struct NewTime
    {
    BOOL Day;
    BOOL Rainy;
    }   NewTime;

which have differently named members of different types. How can I access them from the Assembly automatically? How do I parse them, and create structs in my C# code to set them to a value I want? Below is what I started with, but the best I got was getting a string containing their names, but no idea how I go on then to create my own objects of those types, and have some sort of parser which tells me what members (of which type) it has.

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace CommandWriter
{
    public partial class CommandWriter : Form
    {
        public CommandWriter()
        {
            Type[] allCommands = ReadAllCommands();
            //InitializeComponent();
        }

        private Type[] GetTypesInNamespace(Assembly assembly, string nameSpace)
        {
            Type[] test = assembly.GetTypes();
            return assembly.GetTypes().Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal)).ToArray();
        }

        public Type[] ReadAllCommands()
        {
            var dllFile = new FileInfo(@"VMSystemTypeLib.Interop.dll");
            Assembly vmSystem = Assembly.LoadFile(dllFile.FullName);
            Type[] exported = vmSystem.GetExportedTypes().ToArray();
            return vmSystem.GetTypes().Where(t => t.Name.StartsWith("DP")).ToArray();
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire