✅ Cannot convert List<MyClass> to List<object>
I'm trying to design a custom generic collection editor in Winforms. I'm getting into a bit of a mess with casting the received class type onto a list of objects. I'm not very good with generics, so tried this via List<object>. See my code here:
https://pastebin.com/M2DwziWj
Gives me the attached error (precis: Unable to cast object of type List<SupportApplications> to type List<object>
I'm sure this may be a job for List<T> or something, but I've never really got my head around generics. I know this is me getting some basic principles wrong. Can anyone help with example code?
Pastebin
public class SupportApplications: object{ public string Name { get;...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

37 Replies
List is invariant, because otherwise you could do something like this
Yes - that makes sense. How should I approach this then?
You could make the form generic I guess?
Like I say - I'm rubbish with generics. I tried this in
GenericCollectionEditorForm
:
but get the error "namespace T cannot be found"
oh wait!
works¬
!Yep, you need to get the
T
from somewhere for it to workhow does one add a new object of type T to a list? Tried variations on the line below in
GenericCollectionEditorForm<T> : FormViewBase
, but no dice:
Also, Objects.Remove(objectsBindingSource.Current);
doesn't compile - "cannot convert from object to T"Just... add that object?
If
T
is int
, just add an int
If T
is Person
, just add an instance of Person
If T
is IFoo
then add an instance of any class that implements IFoo
You get the ideabut that defeats the point for the exercise - it's a generic collection editor - that is - GenericCollectionEditorForm won't know what type is being passed in - can you not achieve this via T?
Why won't it know the type? Just instantiate
GenericCollectionEditorForm
with a given type
You have to, to even create the instanceIt sounds like they want to be able to make new Ts inside the editor?
@Angius Code as currently stands:
Objects.Add((T)Activator.CreateInstance(typeof(T), new object()));
untested at runtime as getting compile error on Objects.Remove(objectsBindingSource.Current);
- "cannot convert from object to T"You can just cast.
lol - oh ffs
I'm dumb
Now stuck on instantiating it. The variable
type
shows no errors, but cannot get the line below it right:
List<T>
, assuming the type
is the same as T
You can't pass an instance of type
as a generic parameterOK. Forget type. How would I instantiate GenericCollectionEditorForm? (have removed 'type' as a parameter):
Last line not working in many places!
Uh
GenericCollectionEditorForm((T)value)
...?
We're firmly entering "weird shit" territory lolSorry Zzzz - I'm a hobby coder - I know you're a super-duper expert coder and everything - so my amateurship may be throwing you
tried:
GenericCollectionEditorForm<Type> form = new GenericCollectionEditorForm((T)value);
LHS worked, but RHS doesn't compile:

GenericCollectionEditorForm<T> form = new GenericCollectionEditorForm((T)value);
Or just var form = new GenericCollectionEditorForm((T)value);
for simplicitynaw - whichever permutation, it doens't like
GenericCollectionEditorForm((T)value)
it also doesn't like GenericCollectionEditorForm<T>
!Are we still inside of
GenericCollectionEditorForm
class, or somewhere else?
Like, are you trying to create a new GenericCollectionEditorForm
from another form?Yeah soz - now in
GenericCollectionEditor
which instantiates the form. It may be helpful for you to review my OP code - which shows the full structure.Right, well, the generic type has to come from somewhere
So you can either define it here,
Or it needs to come from... somewhere
Be it a generic
EditValue<T>
or a generic GenericCollectionEditor<T>
Let's focus the issue:
This line compiles, but erro at runtime:
enericCollectionEditorForm<Type> form = new GenericCollectionEditorForm<Type>((List<Type>)value);
Well, you're creating an editor form for a list of
Type
instances
Type
instance is not the same as a genericyes I know! The question is again, how do I instantiate the form from what I recieve in the EditValue method? Are you familiar with UITypeEditor first of all?
The gist of it is: if you want to use generic types, the generic must be known at some point. You can't do
nor can you do
UITypeEditorHonestly, no, first time I hear of it
ah OK - well that may be part of the communicaiton break down. One moment
From what I can see in the docs (https://learn.microsoft.com/en-us/dotnet/api/system.drawing.design.uitypeeditor?view=windowsdesktop-9.0) it's not really generic
I see a bunch of casting and
typeof
s
So you might have to just forgo type-safety here and yolo it with typeof
s, reflections, and object
s
Ancient .NET APIs were... not always well thought-outOK - so this may help you to help me (I do appreciate your efforts). Think property grid in Vis Studio. If you decorate a Property of an object with a
UITypeEditor
attribute, then when you edit it in the property grid, the class derived from UITypeEditor
is called (you don't manually call it yourself).
The value
parameter of public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
holds the value of the property you want to edit. Thus this could be antyhning - a List, a string, an int etc.
Have just tested my code. the line Type type = value.GetType().GetGenericArguments().SingleOrDefault() ?? typeof(object);
successfully detemrines the Type of value
Now the remaining question is, how do I instantiate GenericCollectionEditorForm given I know the Type of value
at runtime?
Specifically - what is the syntax?Jusr forgo the generics and pass
object
that is your value
Pass the type intance as well if needed, sureno - that takes us right back to the very beginning again! See JCotton42's first comment!
But any generic types like lists will have to be
List<object>
or constructed with reflectionsnevermind - just leave it - maybe someone else can help
Well, if you have no generic type, you have no generic type
The "source" only has an
object
instance
You cannot, in any way shape or form, get a generic parameter out of it
You could, maybe, cast all elements of the list to object
in order to get the List<object>
you need
theList.Cast<object>()
So that wherever it is the original error happens is happySolved this for any that are in a similar boat. Found some Reflection voodoo that instantiated x from the determined Type:
Full code here: https://pastebin.com/gLHd2jqX
Pastebin
internal class GenericCollectionEditor : UITypeEditor { ...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
yes, you have to do that.
or make factories, put them in a dictionary and look up by type, if you want more flexibility in how they are created
idk why no one mentioned this
the form might be doing type checks on the generic inside to select behavior, this might break