Giter Site home page Giter Site logo

dynamicstruct's Introduction

This project contains two MS VS2010 Solutions: a simple class library that uses reflection to create a struct from an XML file, and a simple WinForms app that uses the library to create the struct in memory and tells you if it worked (or not). 

For example, you have a struct in which you won't know the field names or data types until runtime. But let's say that at runtime, you want a struct that look like this:

using System;
using System.Runtime.InteropServices;

namespace MyGreatProject
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    struct MyCoolStruct
    {
        public System.Double Latitude;
        public System.Double Longitude;
        public System.Int32 Altitude;
    }
}

The code supplied will create that struct for you in memory, assuming it finds an XML file like this:

<?xml version="1.0" encoding="utf-8"?>
<StructFieldCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <DataSet>
    <StructField>
      <Name>Latitude</Name>
      <DataType>System.Double</DataType>
    </StructField>
    <StructField>
      <Name>Longitude</Name>
      <DataType>System.Double</DataType>
    </StructField>
    <StructField>
      <Name>Altitude</Name>
      <DataType>System.Int32</DataType>
    </StructField>
  </DataSet>
</StructFieldCollection>

What could you use this for? Hmm... 

I use it for a vendor-specific API that requires a struct defining each of the (possibly hundreds) of variables you want to read to/write from their system. Keeping this straight and trying to quickly test the addition/removal of a field or two got unmanageable. 

But you could pass Windows messages (WM_COPYDATA / Marshal.PtrToStructure...) from one app to another without having to compile each app every time you changed the structure of the data in the message. (Of course you could do this also with UDP packets sent to/from localhost, or a message bus, or a shared database, or possibly even smoke signals. But that's for another project...)

dynamicstruct's People

Contributors

jasonbrice avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.