locked
How to display diffrent versions of an interface RRS feed

  • Question

  •  I need the ability to load dynamically an assembly that holds a class with a public struct (used to interface with another application)
    and display that struct (feilds name and types - using reflection).
    The problem is that i can not load this class dynamically i need to referance it and by that i lose the ability to display diffents versions of the same struct.
    navo
    Sunday, July 6, 2008 1:47 PM

Answers

  • Navo.

    Here's an example of how you can dynamically load an assembly and use reflection to manipulate struct in that assembly.  I hope it's of some use:

    1using System; 
    2using System.IO; 
    3using System.Reflection; 
    4 
    5namespace Green 
    6
    7    class Grass 
    8    { 
    9 
    10        public static void Main() 
    11        { 
    12            // First the usual way. 
    13            Bottle.Label label1 = new Bottle.Label("Coke", DateTime.UtcNow.AddDays(1)); 
    14            Console.WriteLine(label1.GetExpirationText()); 
    15 
    16            // Now do the same using external assembly 
    17 
    18            //load the assembly 
    19            string filePath = new FileInfo("green.exe").FullName; 
    20            Assembly assemb = Assembly.LoadFile(filePath); 
    21 
    22            //get the type of the label 
    23            Type labelType = assemb.GetType("Green.Bottle").GetNestedType("Label"); 
    24 
    25            //Get the constuctor for a new label 
    26            Type[] constructorSignature = new Type[] { typeof(string), typeof(DateTime) }; 
    27            ConstructorInfo constructor = labelType.GetConstructor(constructorSignature); 
    28 
    29            //Constructor a new label 
    30            object label2 = constructor.Invoke(new object[] { "Clorox", DateTime.UtcNow.AddYears(5) }); 
    31 
    32            //Get the method we want 
    33            MethodInfo expirationTextMethod = labelType.GetMethod("GetExpirationText"); 
    34            string labelText = (string)expirationTextMethod.Invoke(label2, new object[0]); 
    35 
    36            Console.WriteLine(labelText); 
    37 
    38            Console.ReadKey(); 
    39        } 
    40 
    41    } 
    42 
    43    class Bottle 
    44    { 
    45        public struct Label 
    46        { 
    47            public Label(string productName, DateTime sellBy) 
    48            { 
    49                ProductName = productName; 
    50                SellBy = sellBy; 
    51            } 
    52 
    53            public string ProductName; 
    54            public DateTime SellBy; 
    55 
    56            public string GetExpirationText() 
    57            { 
    58                return string.Concat("This bottle of ", ProductName, " will expire on ", SellBy.ToString("dd MMM yyyy")); 
    59            } 
    60        } 
    61    } 
    62 
    63
    64 
    65//Output: 
    66 
    67//This bottle of Coke will expire on 07 Jul 2008 
    68//This bottle of Clorox will expire on 06 Jul 2013 


    Good luck,

    John
    • Marked as answer by Zhi-Xin Ye Thursday, July 10, 2008 8:21 AM
    Sunday, July 6, 2008 5:10 PM
  • You are using the terms interface and struct interchangeably.  A struct is not an interface and doesn't help you making your code resilient to versioning.  Start with an interface declaration in a separate assembly that both your main program and your "plug-in" reference.  Let your main program only refer to the plug-in class implementation through the interface.  You are free to modify the class in your plug-in as needed.  As long as you don't change the interface.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Thursday, July 10, 2008 8:21 AM
    Sunday, July 6, 2008 5:26 PM

All replies

  • Navo.

    Here's an example of how you can dynamically load an assembly and use reflection to manipulate struct in that assembly.  I hope it's of some use:

    1using System; 
    2using System.IO; 
    3using System.Reflection; 
    4 
    5namespace Green 
    6
    7    class Grass 
    8    { 
    9 
    10        public static void Main() 
    11        { 
    12            // First the usual way. 
    13            Bottle.Label label1 = new Bottle.Label("Coke", DateTime.UtcNow.AddDays(1)); 
    14            Console.WriteLine(label1.GetExpirationText()); 
    15 
    16            // Now do the same using external assembly 
    17 
    18            //load the assembly 
    19            string filePath = new FileInfo("green.exe").FullName; 
    20            Assembly assemb = Assembly.LoadFile(filePath); 
    21 
    22            //get the type of the label 
    23            Type labelType = assemb.GetType("Green.Bottle").GetNestedType("Label"); 
    24 
    25            //Get the constuctor for a new label 
    26            Type[] constructorSignature = new Type[] { typeof(string), typeof(DateTime) }; 
    27            ConstructorInfo constructor = labelType.GetConstructor(constructorSignature); 
    28 
    29            //Constructor a new label 
    30            object label2 = constructor.Invoke(new object[] { "Clorox", DateTime.UtcNow.AddYears(5) }); 
    31 
    32            //Get the method we want 
    33            MethodInfo expirationTextMethod = labelType.GetMethod("GetExpirationText"); 
    34            string labelText = (string)expirationTextMethod.Invoke(label2, new object[0]); 
    35 
    36            Console.WriteLine(labelText); 
    37 
    38            Console.ReadKey(); 
    39        } 
    40 
    41    } 
    42 
    43    class Bottle 
    44    { 
    45        public struct Label 
    46        { 
    47            public Label(string productName, DateTime sellBy) 
    48            { 
    49                ProductName = productName; 
    50                SellBy = sellBy; 
    51            } 
    52 
    53            public string ProductName; 
    54            public DateTime SellBy; 
    55 
    56            public string GetExpirationText() 
    57            { 
    58                return string.Concat("This bottle of ", ProductName, " will expire on ", SellBy.ToString("dd MMM yyyy")); 
    59            } 
    60        } 
    61    } 
    62 
    63
    64 
    65//Output: 
    66 
    67//This bottle of Coke will expire on 07 Jul 2008 
    68//This bottle of Clorox will expire on 06 Jul 2013 


    Good luck,

    John
    • Marked as answer by Zhi-Xin Ye Thursday, July 10, 2008 8:21 AM
    Sunday, July 6, 2008 5:10 PM
  • You are using the terms interface and struct interchangeably.  A struct is not an interface and doesn't help you making your code resilient to versioning.  Start with an interface declaration in a separate assembly that both your main program and your "plug-in" reference.  Let your main program only refer to the plug-in class implementation through the interface.  You are free to modify the class in your plug-in as needed.  As long as you don't change the interface.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Thursday, July 10, 2008 8:21 AM
    Sunday, July 6, 2008 5:26 PM