Click here to Skip to main content
15,917,320 members
Home / Discussions / C#
   

C#

 
GeneralRe: To be liberal or conservative with virtual methods... [modified] Pin
PIEBALDconsult8-Dec-09 14:46
mvePIEBALDconsult8-Dec-09 14:46 
GeneralRe: To be liberal or conservative with virtual methods... Pin
Jimmanuel8-Dec-09 15:34
Jimmanuel8-Dec-09 15:34 
GeneralRe: To be liberal or conservative with virtual methods... [modified] Pin
PIEBALDconsult8-Dec-09 16:26
mvePIEBALDconsult8-Dec-09 16:26 
AnswerRe: To be liberal or conservative with virtual methods... Pin
Daniel Grunwald8-Dec-09 15:05
Daniel Grunwald8-Dec-09 15:05 
GeneralRe: To be liberal or conservative with virtual methods... Pin
PIEBALDconsult8-Dec-09 17:44
mvePIEBALDconsult8-Dec-09 17:44 
QuestionCheckedListBox state to property Pin
BDJones8-Dec-09 7:06
BDJones8-Dec-09 7:06 
AnswerRe: CheckedListBox state to property Pin
PIEBALDconsult8-Dec-09 7:32
mvePIEBALDconsult8-Dec-09 7:32 
AnswerRe: CheckedListBox state to property Pin
dan!sh 8-Dec-09 8:03
professional dan!sh 8-Dec-09 8:03 
QuestionRe: CheckedListBox state to property Pin
BDJones8-Dec-09 8:44
BDJones8-Dec-09 8:44 
AnswerRe: CheckedListBox state to property Pin
PIEBALDconsult8-Dec-09 10:30
mvePIEBALDconsult8-Dec-09 10:30 
AnswerRe: CheckedListBox state to property Pin
dan!sh 8-Dec-09 16:27
professional dan!sh 8-Dec-09 16:27 
Questionany other better way for window form to interact with excel? Pin
neodeaths8-Dec-09 4:39
neodeaths8-Dec-09 4:39 
AnswerRe: any other better way for window form to interact with excel? Pin
Giorgi Dalakishvili8-Dec-09 5:19
mentorGiorgi Dalakishvili8-Dec-09 5:19 
GeneralRe: any other better way for window form to interact with excel? Pin
neodeaths14-Dec-09 20:17
neodeaths14-Dec-09 20:17 
GeneralRe: any other better way for window form to interact with excel? Pin
Giorgi Dalakishvili15-Dec-09 5:19
mentorGiorgi Dalakishvili15-Dec-09 5:19 
QuestionDrop and redefine a type in a dynamic module ? Pin
Duncan Edwards Jones8-Dec-09 4:00
professionalDuncan Edwards Jones8-Dec-09 4:00 
Hi
I am dynamically declaring types (using ModuleBuilder.DefineType[^] which are used to allow for strongly typed data sets from a CSV file.

However when the user adds or alters a column definition I'd like to remove and rebuild the data type. Any ideas how I drop and recreate a type - do I have to unload the whole assembly or how is it done?

public static Type MakeTypeForColumns(string typeName, MarketRecObjects.ImportFileFormatColumnDefinitionCollection columns)
{
    Type columnType = null;

    TypeBuilder columnTypeBuilder = GetTypeBuilder(typeName);


    if (null != columnTypeBuilder)
    {
        foreach (ImportFileFormatColumnDefinition col in columns)
        {
            // Create the backing field for the property
            FieldBuilder colFieldBuilder = columnTypeBuilder.DefineField(MakeValidObjectCodeName(@"_" + col.ColumnName),
                col.GetTypeForContentType(), FieldAttributes.Private);

            // Create the property itself
            PropertyBuilder colPropertyBuilder = columnTypeBuilder.DefineProperty(MakeValidObjectCodeName(col.ColumnName),
                PropertyAttributes.None, col.GetTypeForContentType(),
                Type.EmptyTypes);

            // and make a setter/getter for the property
            MethodBuilder colGetMethodBuilder = columnTypeBuilder.DefineMethod(@"get_" + colPropertyBuilder.Name, MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName, colFieldBuilder.FieldType, Type.EmptyTypes);
            ILGenerator colGetMethodGenerator = colGetMethodBuilder.GetILGenerator();
            // return colField
            colGetMethodGenerator.Emit(OpCodes.Ldarg_0);
            colGetMethodGenerator.Emit(OpCodes.Ldfld, colFieldBuilder);
            colGetMethodGenerator.Emit(OpCodes.Ret);

            MethodBuilder colSetMethodBuilder = columnTypeBuilder.DefineMethod(@"set_" + colPropertyBuilder.Name, MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName, typeof(void), new Type[] { colFieldBuilder.FieldType });
            ILGenerator colSetMethodGenerator = colSetMethodBuilder.GetILGenerator();
            // set colField = value
            colSetMethodGenerator.Emit(OpCodes.Ldarg_0);
            colSetMethodGenerator.Emit(OpCodes.Ldarg_1);
            colSetMethodGenerator.Emit(OpCodes.Stfld, colFieldBuilder);
            colSetMethodGenerator.Emit(OpCodes.Ret);

            colPropertyBuilder.SetGetMethod(colGetMethodBuilder);
            colPropertyBuilder.SetSetMethod(colSetMethodBuilder);

        }
        // Create an empty constructor for the class
        ConstructorBuilder columnConstructorBuilder = columnTypeBuilder.DefineConstructor(MethodAttributes.Public,
            CallingConventions.Standard,
            Type.EmptyTypes);
        // Create the constructor body...
        ILGenerator constructorGenerator = columnConstructorBuilder.GetILGenerator();
        constructorGenerator.Emit(OpCodes.Ret);

        columnType = columnTypeBuilder.CreateType();
    }
    else
    {
        columnType = ModuleBuilder.GetType(MakeValidObjectCodeName(typeName), false , false ) ;
    }
    return columnType;
}


where the type is defined by
private static TypeBuilder GetTypeBuilder(string newTypeName)
{
    if (ModuleBuilder.GetType(MakeValidObjectCodeName(newTypeName), false, true) == null)
    {
        try
        {
            TypeBuilder ret = ModuleBuilder.DefineType(MakeValidObjectCodeName(newTypeName), TypeAttributes.Public | TypeAttributes.Sealed);
            return ret;
        }
        catch (System.ArgumentException argEx)
        {
            // We have a duplicate type name....
            System.Diagnostics.Trace.TraceError(argEx.ToString());
            return null;
        }
    }
    else
    {
        return null;
    }
}


'--8<------------------------
Ex Datis:
Duncan Jones
Merrion Computing Ltd

AnswerRe: Drop and redefine a type in a dynamic module ? Pin
Luc Pattyn8-Dec-09 5:05
sitebuilderLuc Pattyn8-Dec-09 5:05 
GeneralRe: Drop and redefine a type in a dynamic module ? Pin
Duncan Edwards Jones8-Dec-09 7:10
professionalDuncan Edwards Jones8-Dec-09 7:10 
GeneralRe: Drop and redefine a type in a dynamic module ? Pin
Luc Pattyn8-Dec-09 7:47
sitebuilderLuc Pattyn8-Dec-09 7:47 
QuestionCommunication between .NET 2 Assemblies and .NET 3.5 Assemblies Pin
softwarejaeger8-Dec-09 3:09
softwarejaeger8-Dec-09 3:09 
AnswerRe: Communication between .NET 2 Assemblies and .NET 3.5 Assemblies Pin
Luc Pattyn8-Dec-09 5:08
sitebuilderLuc Pattyn8-Dec-09 5:08 
GeneralRe: Communication between .NET 2 Assemblies and .NET 3.5 Assemblies Pin
softwarejaeger8-Dec-09 5:26
softwarejaeger8-Dec-09 5:26 
GeneralRe: Communication between .NET 2 Assemblies and .NET 3.5 Assemblies Pin
Daniel Grunwald8-Dec-09 15:19
Daniel Grunwald8-Dec-09 15:19 
AnswerRe: Communication between .NET 2 Assemblies and .NET 3.5 Assemblies Pin
PIEBALDconsult8-Dec-09 14:23
mvePIEBALDconsult8-Dec-09 14:23 
Questioninaccessible due to protection level Pin
Jassim Rahma8-Dec-09 2:17
Jassim Rahma8-Dec-09 2:17 

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.