UIType Editor throws object reference

71 views Asked by At

I have a .dll file where I have implemented my custom controls and the type editor.

the UITypeEditor works in the .dll file. however when I import the .dll to the main project it give me an object reference error.

My Custom Form and the Type editor is below:

Form:

public partial class MyBrowseForm<TParentEntity> : Base4Form, IBaseBrowseDialog where TParentEntity : class
{

[Category("Base4Data")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Editor(typeof(BdFormTypeEditor), typeof(UITypeEditor))]       
    [TypeConverter(typeof(CollectionConverter))]
    public BindingList<MySearchFields> MySearchFieldsCollection
    {
        get => _mySearchFieldsCollection;
        set => _mySearchFieldsCollection = value;
    }

[Browsable(false)]
    public Type MyGenericType => typeof(TParentEntity);
}

UITypeEdior

internal class BdFormTypeEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {

        var svc = provider?.GetService(typeof(IWindowsFormsEditorService))
            as IWindowsFormsEditorService;

        if (context != null)
        {
            var genericType = ((dynamic) context.Instance).MyGenericType;



            var editorFormType = typeof(MyEditorForm<>);

            var editorFormInstance = editorFormType?.MakeGenericType(genericType);
            if (svc != null)
            {
                using (var f = (Form) Activator.CreateInstance(editorFormInstance, null))
                {
                    f.GetType().GetProperty("List")?.SetValue(f, value);
                    if (svc.ShowDialog(f) == DialogResult.OK)
                        return ((dynamic) f).List;
                }
            }
            else
            {
                using (var f = (Form) Activator.CreateInstance(editorFormInstance, null))
                {
                    f.GetType().GetProperty("List")?.SetValue(f, value);
                    if (svc.ShowDialog(f) == DialogResult.OK)
                        return ((dynamic) f).List;
                }
            }
        }
        return base.EditValue(context, provider, value);
    }
}

I have created another UITypeEditor and a Form to open from the Typeeditor. (using statement above) this time I have added the following code only and I'm not passing values to any property in the form. and I get the same error.

var genericType = (Type)context?.Instance.GetPropValue("MyGenericType");

        //var genericArgument = (Type)myGenericTypeProperty.GetValue(context.Instance);
        var editorFormType = typeof(MyEditorForm<>);
        //var genericArguments = new[] { genericArgument };
        var editorFormInstance = editorFormType.MakeGenericType(genericType);

        if (svc != null)
        {
            using (var f = Form)Activator.CreateInstance(editorFormInstance))

so this works in the original solution but when I build and import the dll to another solution I get an object reference..

What do you think I'm missing here? any help is appreciated.

0

There are 0 answers