Click here to Skip to main content
15,886,572 members
Home / Discussions / C#
   

C#

 
AnswerRe: Top books for learning C# (Complete novice) for thermodynamic mathematical modelling Pin
Matt T Heffron27-Nov-12 12:59
professionalMatt T Heffron27-Nov-12 12:59 
GeneralRe: Top books for learning C# (Complete novice) for thermodynamic mathematical modelling Pin
Mahdi Kapateh28-Nov-12 3:04
Mahdi Kapateh28-Nov-12 3:04 
QuestionDynamically loading a plug-in assembly by interface Pin
MJL Rademakers27-Nov-12 4:07
MJL Rademakers27-Nov-12 4:07 
AnswerRe: Dynamically loading a plug-in assembly by interface Pin
Alan N27-Nov-12 4:27
Alan N27-Nov-12 4:27 
GeneralRe: Dynamically loading a plug-in assembly by interface Pin
MJL Rademakers27-Nov-12 4:41
MJL Rademakers27-Nov-12 4:41 
AnswerRe: Dynamically loading a plug-in assembly by interface Pin
Pete O'Hanlon27-Nov-12 4:30
mvePete O'Hanlon27-Nov-12 4:30 
GeneralRe: Dynamically loading a plug-in assembly by interface Pin
MJL Rademakers27-Nov-12 4:45
MJL Rademakers27-Nov-12 4:45 
AnswerRe: Dynamically loading a plug-in assembly by interface Pin
PIEBALDconsult27-Nov-12 8:17
mvePIEBALDconsult27-Nov-12 8:17 
MJL Rademakers wrote:
Activator.CreateInstance()


Dead | X|

I never use that, here's what I currently use:

namespace PIEBALD.Lib
{
    public static partial class LibSys
    {
        public static System.Reflection.Assembly
        DynamicLoad
        (
            string Filename
        )
        {
            System.Reflection.Assembly result ;

            System.IO.FileInfo fil = PIEBALD.Lib.LibFil.GetExpandedFileInfo ( Filename ) ;

            try
            {
                /*\
                |*| This is the common way to load an assembly:
                |*| result = System.Reflection.Assembly.LoadFrom ( Filename ) ;
                |*|
                |*| The following is my take on a technique suggested by Sacha Barber:
                \*/

                result = System.AppDomain.CreateDomain 
                    ( System.IO.Path.GetFileNameWithoutExtension ( fil.Name ) ).
                    Load ( System.IO.File.ReadAllBytes ( fil.FullName ) ) ;
            }
            catch ( System.Exception err )
            {
                throw ( new System.InvalidOperationException
                (
                    System.String.Format
                    (
                        "Could not load an assembly from file {0}"
                    ,
                        Filename
                    )
                ,
                    err
                ) ) ;
            }

            return ( result ) ;
        }
    }

    public static partial class LibSys
    {
        public static T
        DynamicLoad<T>
        (
            string          Filename
        ,
            string          Typename
        ,
            params object[] Parameters
        )
        {
            T result = default(T) ;
            
            System.Reflection.Assembly assm = DynamicLoad ( Filename ) ;

            System.Type type = assm.GetType ( Typename ) ;

            if ( type == null )
            {
                throw ( new System.InvalidOperationException
                (
                    System.String.Format
                    (
                        "The assembly in file {0} does not contain a public type named {1}"
                    ,
                        Filename
                    ,
                        Typename
                    )
                ) ) ;
            }

            if ( !typeof(T).IsAssignableFrom ( type ) )
            {
                throw ( new System.InvalidOperationException
                (
                    System.String.Format
                    (
                        "Type {0} in file {1} does not implement or derive from {2}"
                    ,
                        type.Name
                    ,
                        Filename
                    ,
                        typeof(T).FullName
                    )
                ) ) ;
            }

            System.Reflection.ConstructorInfo cons = type.GetConstructor
            (
                PIEBALD.Lib.LibSys.GetTypes ( Parameters ).ToArray()
            ) ;

            if ( cons == null )
            {
                throw ( new System.InvalidOperationException
                (
                    System.String.Format
                    (
                        "Type {0} in file {1} does not have the specified constructor"
                    ,
                        type.Name
                    ,
                        Filename
                    )
                ) ) ;
            }

            try
            {
                result = (T) cons.Invoke
                (
                    Parameters
                ) ;
            }
            catch ( System.Exception err )
            {
                throw ( new System.InvalidOperationException
                (
                    System.String.Format
                    (
                        "Unable to instantiate a {0}"
                    ,
                        type.FullName
                    ) 
                ,
                    err
                ) ) ;
            }

            return ( result ) ;
        }
    }
}

GeneralRe: Dynamically loading a plug-in assembly by interface Pin
BobJanova28-Nov-12 23:49
BobJanova28-Nov-12 23:49 
GeneralRe: Dynamically loading a plug-in assembly by interface Pin
PIEBALDconsult29-Nov-12 2:41
mvePIEBALDconsult29-Nov-12 2:41 
GeneralRe: Dynamically loading a plug-in assembly by interface Pin
BobJanova29-Nov-12 3:12
BobJanova29-Nov-12 3:12 
GeneralRe: Dynamically loading a plug-in assembly by interface Pin
PIEBALDconsult29-Nov-12 5:58
mvePIEBALDconsult29-Nov-12 5:58 
Questionhandle script error on virtual machine Pin
belea1727-Nov-12 3:35
belea1727-Nov-12 3:35 
AnswerRe: handle script error on virtual machine Pin
Richard MacCutchan27-Nov-12 4:29
mveRichard MacCutchan27-Nov-12 4:29 
GeneralRe: handle script error on virtual machine Pin
belea1727-Nov-12 9:27
belea1727-Nov-12 9:27 
AnswerRe: handle script error on virtual machine Pin
Eddy Vluggen27-Nov-12 13:49
professionalEddy Vluggen27-Nov-12 13:49 
QuestionEn- or disabling labels regarding to checked items in a listbox Pin
Dennis Bork27-Nov-12 3:15
Dennis Bork27-Nov-12 3:15 
AnswerRe: En- or disabling labels regarding to checked items in a listbox Pin
Alan N27-Nov-12 4:36
Alan N27-Nov-12 4:36 
AnswerRe: En- or disabling labels regarding to checked items in a listbox Pin
Richard MacCutchan27-Nov-12 4:52
mveRichard MacCutchan27-Nov-12 4:52 
AnswerRe: En- or disabling labels regarding to checked items in a listbox Pin
Nilesh Raval27-Nov-12 19:22
professionalNilesh Raval27-Nov-12 19:22 
GeneralRe: En- or disabling labels regarding to checked items in a listbox Pin
Dennis Bork28-Nov-12 0:05
Dennis Bork28-Nov-12 0:05 
GeneralRe: En- or disabling labels regarding to checked items in a listbox Pin
Nilesh Raval28-Nov-12 1:05
professionalNilesh Raval28-Nov-12 1:05 
QuestionUnsupported Operation. A document processed by the JRC engine cannot be opened in the C++ stack. Pin
MAHAMADLATIF HASAN SANADI27-Nov-12 0:41
MAHAMADLATIF HASAN SANADI27-Nov-12 0:41 
AnswerRe: Unsupported Operation. A document processed by the JRC engine cannot be opened in the C++ stack. Pin
Richard Deeming27-Nov-12 1:06
mveRichard Deeming27-Nov-12 1:06 
Questionhow to load values into combobox Pin
chahthuranga26-Nov-12 22:06
chahthuranga26-Nov-12 22:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.