Custom TypeConverter Not Loading in Designer

Posts   
 
    
usschad
User
Posts: 77
Joined: 11-Sep-2008
# Posted on: 03-Apr-2024 17:53:11   

LLBLGen v5.11.1 (19-Dec-2023)

Adapter Template / .NET 6

Azure SQL Database

I have created a new TypeConverter following instructions from: https://www.llblgen.com/Documentation/5.0/SDK/gui_implementingtypeconverter.htm

Converter Purpose (Make sure empty strings or whitespace get converted to NULL when saved to the DB; When reading, convert it back to Empty String).

I dropped it in my TypeConverters folder in the LLBLGen Installation Directory. Opened LLBLGen, went to Project Settings => Type Conversions => Selected String type I expected the list of converters to show my new converter, but it only showed the ones that come with LLBLGen.

I followed the instructions to debug (build in debug mode with PDBs and copied into the directory, attached to the llblgen.exe and set breakpoints on the 3 methods that get called by the designer; but the breakpoints were never caught.

Do you have any advice on what I am doing wrong?

Thanks in advance!  

using System;
using System.ComponentModel;

namespace My.Data.LLBL.TypeConverters
{
    [Description("Ensures Emtpy String or Whitespace values are converted to NULL in the Database. Will convert NULLS from Database to string.Empty.")]
    public class StringConverter : TypeConverter
    {
        public StringConverter()  {}

        public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
        {
            switch (sourceType.FullName)
            {
                case "System.String":
                    return true;
                default:
                    return false;
            }
        }

        public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
        {
            switch (destinationType?.FullName)
            {
                case "System.String":
                    return true;
                default:
                    return false;
            }
        }

        public override object ConvertFrom(ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value)
        {
            if (value is not string)
                    throw new NotSupportedException("Conversion from a value of type '" + value.GetType().ToString() + "' to System.String isn't supported");
            string toReturn = value as string ?? string.Empty;
            return toReturn;
        }

        public override object ConvertTo(ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, Type destinationType)
        {
            if (value is not null && value is not string)
                    throw new NotSupportedException("Conversion from a value of type '" + value.GetType().ToString() + "' to System.String isn't supported");
            if (value is not null && ((string)value).Trim() == string.Empty)
                    value = null;
            return value as string;
        }

        public override object CreateInstance(ITypeDescriptorContext? context, System.Collections.IDictionary? propertyValues)
        {
            return string.Empty;
        }
    }
}
Walaa avatar
Walaa
Support Team
Posts: 14986
Joined: 21-Aug-2005
# Posted on: 03-Apr-2024 22:56:49   

Against which .NET version was the TypeConverter assembly compiled?

usschad
User
Posts: 77
Joined: 11-Sep-2008
# Posted on: 04-Apr-2024 02:17:51   

Walaa wrote:

Against which .NET version was the TypeConverter assembly compiled?

.NET 6

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 04-Apr-2024 09:17:31   

The type converters for the designer have to be compiled against .NET 4.8 or .netstandard 2.0, as the designer is a .net framework application. You can specify multiple target frameworks for the csproj so you don't have to do double work.

Frans Bouma | Lead developer LLBLGen Pro
usschad
User
Posts: 77
Joined: 11-Sep-2008
# Posted on: 04-Apr-2024 16:13:05   

How embarrassing! lol

Yeah, that worked.

For posterity:

I opened my csproj file and changed removed the TargetFramework tag and replaced it with

<TargetFrameworks>net48;net6.0</TargetFrameworks>

I went to my build folder of my TypeConverter project: \bin\Debug\net48 ... copied the .dll and .pdb into the LLBLGen TypeConverters directory (i.e. \Solutions Design\LLBLGen Pro v5.11\TypeConverters)

Opened the designer, viewed Project > Settings > Type Conversions > Add New > Selected String ... and my Type Converter was listed.

Going to run some integration tests now to see if everything works as expected.

Thanks for the help!

Note: In my original post, I had a bug. The CreateInstance method returned true (from the example of a Boolean Converter I copied from the docs). I didn't realize it needed to return something that was the type we were converting to. Once I changed it to string.Empty, it appeared correctly in the designer.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 05-Apr-2024 08:46:37   

Glad it's solved! simple_smile

Frans Bouma | Lead developer LLBLGen Pro