.NET Forum / Languages / C# / January 2008
The value mytype is not of type mytype exception
|
|
Thread rating:  |
Andrus - 20 Jan 2008 23:20 GMT I have code in Winforms 2 combobox:
EntityBase entity = EntityManager.FindById(LookupTableName, id); IList entityList = DataSource as IList; entityList.Add(entity);
Last line causes strange exception below.
Entity variable type is MyApp.EntityExtension.Klliik which is inherited from my EntityBase type so this must work. It works OK but some code refactoring breaks it today. No idea why. I tried to use cast
ArrayList entityList = DataSource as ArrayList;
but entityList becomes null if this line is used.
Exception message says that type a is not type a . Is'nt this message wrong ? How to fix this code ?
Andrus
System.ArgumentException was unhandled by user code Message="The value \"MyApp.EntityExtension.Klliik\" is not of type \"MyApp.EntityExtension.Klliik\" and cannot be used in this generic collection.\r\nParameter name: value" Source="mscorlib" ParamName="value" StackTrace: at System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object value, Type targetType) at System.Collections.Generic.List`1.VerifyValueType(Object value) at System.Collections.Generic.List`1.System.Collections.IList.Add(Object item) ....
Peter Duniho - 20 Jan 2008 23:43 GMT > [...] > Exception message says that type a is not type a . > Is'nt this message wrong ? I have no doubt that the message is accurate and correct. In spite of numbers of times that people post thinking that an error message telling them the types are incompatible is wrong, it has never actually turned out to be wrong. It's always the case that the person simply misunderstands their code and that they have in fact tried to use something that is the wrong type.
The most common reason this happens is that they are using a type that was copied and pasted or otherwise duplicated in multiple assemblies. If the type didn't come from the same assembly, it's not the same type.
However, your problem description is so vague and disarrayed that it's impossible to say exactly what's wrong. You don't even mention multiple assemblies, so who knows if that could be related to your problem. That could be the problem, or it could be something else entirely.
> How to fix this code ? Not possible to say. You should post a concise-but-complete sample of code that reliably reproduces the problem. If you post something that is the bare minimum required the reproduce the error, but which contains a _complete_ compilable sample of code, it should be simple enough to point out what's going wrong.
But as long as we have to guess at how you've defined your generic class(es?), or which types are what, or where the "DataSource" member comes from or what it is, or... (I hope you get the idea), there's no way for anyone to comprehend what you've posted in a way that will allow an accurate answer.
Pete
Andrus - 21 Jan 2008 13:43 GMT > The most common reason this happens is that they are using a type that was > copied and pasted or otherwise duplicated in multiple assemblies. If the > type didn't come from the same assembly, it's not the same type. This means that type name is not unique in application. How to change this code so that it orints information from which .net determines this error message.
> However, your problem description is so vague and disarrayed that it's > impossible to say exactly what's wrong. You don't even mention multiple > assemblies, so who knows if that could be related to your problem. That > could be the problem, or it could be something else entirely. I have multiple unsigned assemblies. One assembly from which this type is created is created dynamically at run time (adding GetEmptyList() method) and returned from AssemblyResolve event. I expect .net can use assembly from AssemblyResolve for type resolution. At design time wrapper assembly is used to allow application to build.
>> How to fix this code ? > [quoted text clipped - 3 lines] > _complete_ compilable sample of code, it should be simple enough to point > out what's going wrong. This is a bit of work to extract working code from existing appl which accesses database. If information below is not sufficient please write, I can try to create this.
> But as long as we have to guess at how you've defined your generic > class(es?), or which types are what, or where the "DataSource" member > comes from or what it is, or... (I hope you get the idea), there's no way > for anyone to comprehend what you've posted in a way that will allow an > accurate answer. DataSource is Combobox property. It is set by function GetObjectPickList() which returns
public static object GetObjectPickList() {
return Klliik.GetEmptyList(); }
Klliik class and its GetEmptyList() method is generated dynamically in runtime and contains:
[Table(Name = "klliik")] public class Klliik : Eetasoft.Eeva.Entity.Klliik { public Klliik() : base() { } public Klliik(System.String Liik, System.String Nimetus, System.String Engnimetus, System.String Finnimetus, System.String Lvlnimetus, System.String Rusnimetus) : base(Liik, Nimetus, Engnimetus, Finnimetus, Lvlnimetus, Rusnimetus) {
}
public static List<Klliik> GetEmptyList() { return new List<Klliik>(); }
public static Klliik FindById(string id) { var q = (from k in global::Default.Db.Klliiks where k.Liik == id select k);
foreach (var e in q.ToList()) return e; return null; } }
Andrus.
Peter Duniho - 22 Jan 2008 01:27 GMT > [...] > Klliik class and its GetEmptyList() method is generated dynamically in > runtime and contains: I don't know much about dynamically-created types. However, I suspect that this is your problem. If you are going to use a type in more than one place, each place needs to make sure it is using the exact same type, not just one that happens to look like the other.
This means for a dynamically-created type, I believe you need to make sure you create the type only once, and then use a reference to that once-created type anywhere you actually want use the type.
Your question gets into things like reflection and run-time compilation, which I don't know that much about. Hopefully someone else who knows more can offer something more specific, if the above isn't useful advice.
Pete
Ignacio Machin ( .NET/ C# MVP ) - 21 Jan 2008 13:46 GMT Hi,
It's difficult to say where is the problem, can you compare the refactored code with the one that was workgin before?
Have you use teh debug to see the type of the entity variable?
 Signature Ignacio Machin http://www.laceupsolutions.com Mobile & warehouse Solutions.
>I have code in Winforms 2 combobox: > [quoted text clipped - 33 lines] > item) > .... Andrus - 21 Jan 2008 15:16 GMT > It's difficult to say where is the problem, can you compare the refactored > code with the one that was workgin before? No. I did'nt made backup before refactoring.
> Have you use teh debug to see the type of the entity variable? In immediate window entity.getType() returns:
{Name = "Klliik" FullName = ".EntityExtension.Klliik"} [System.RuntimeType]: {Name = "Klliik" FullName = "MyApp.EntityExtension.Klliik"} base {System.Reflection.MemberInfo}: {Name = "Klliik" FullName = "MyApp.EntityExtension.Klliik"} Assembly: {-fnethhd, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null} AssemblyQualifiedName: "MyApp.EntityExtension.Klliik, -fnethhd, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" Attributes: Public | BeforeFieldInit BaseType: {Name = "Klliik" FullName = "MyApp.Entity.Klliik"} ContainsGenericParameters: false DeclaringMethod: 'entity.GetType().DeclaringMethod' threw an exception of type 'System.InvalidOperationException' DeclaringType: null FullName: "MyApp.EntityExtension.Klliik" GenericParameterAttributes: 'entity.GetType().GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException' GenericParameterPosition: 'entity.GetType().GenericParameterPosition' threw an exception of type 'System.InvalidOperationException' GUID: {fedaafb6-5f8e-37a0-a233-63922bdef875} ... MemberType: TypeInfo Module: {-fnethhd.dll} Namespace: "MyApp.EntityExtension" ReflectedType: null StructLayoutAttribute: {System.Runtime.InteropServices.StructLayoutAttribute} TypeHandle: {System.RuntimeTypeHandle} TypeInitializer: null UnderlyingSystemType: {Name = "Klliik" FullName = "MyApp.EntityExtension.Klliik"}
entityList.GetType() shows:
{Name = "List`1" FullName = "System.Collections.Generic.List`1[[MyApp.EntityExtension.Klliik, wuxled5r, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]"} [System.RuntimeType]: {Name = "List`1" FullName = "System.Collections.Generic.List`1[[MyApp.EntityExtension.Klliik, wuxled5r, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]"} base {System.Reflection.MemberInfo}: {Name = "List`1" FullName = "System.Collections.Generic.List`1[[MyApp.EntityExtension.Klliik, wuxled5r, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]"} Assembly: {mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089} AssemblyQualifiedName: "System.Collections.Generic.List`1[[MyApp.EntityExtension.Klliik, wuxled5r, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" Attributes: Public | Serializable | BeforeFieldInit BaseType: {Name = "Object" FullName = "System.Object"} ContainsGenericParameters: false DeclaringMethod: 'entityList.GetType().DeclaringMethod' threw an exception of type 'System.InvalidOperationException' DeclaringType: null FullName: "System.Collections.Generic.List`1[[MyApp.EntityExtension.Klliik, wuxled5r, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]" GenericParameterAttributes: 'entityList.GetType().GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException' GenericParameterPosition: 'entityList.GetType().GenericParameterPosition' threw an exception of type 'System.InvalidOperationException' GUID: {5af00c2e-048b-3ab3-8ccd-548134771d38} ... MemberType: TypeInfo Module: {CommonLanguageRuntimeLibrary} Namespace: "System.Collections.Generic" ReflectedType: null StructLayoutAttribute: {System.Runtime.InteropServices.StructLayoutAttribute} TypeHandle: {System.RuntimeTypeHandle} TypeInitializer: {Void .cctor()} UnderlyingSystemType: {Name = "List`1" FullName = "System.Collections.Generic.List`1[[MyApp.EntityExtension.Klliik, wuxled5r, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]"}
In summary
entity : MyApp.EntityExtension.Klliik, -fnethhd, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null entityList member: MyApp.EntityExtension.Klliik, wuxled5r, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
will different assembly names cause this error ?
If so, I have created only one dynamic assembly. This assembly contains both Klliik type and GetEmptyList() function used to initialize entityList:
[Table(Name = "klliik")] public class Klliik : MyApp.Entity.Klliik { public Klliik() : base() { } public Klliik(System.String Liik, System.String Nimetus, System.String Engnimetus, System.String Finnimetus, System.String Lvlnimetus, System.String Rusnimetus) : base(Liik, Nimetus, Engnimetus, Finnimetus, Lvlnimetus, Rusnimetus) { } public static List<Klliik> GetEmptyList() { return new List<Klliik>(); }
Why list member created from this assembly has different module name and type created from this assembly ?
Andrus.
Ignacio Machin ( .NET/ C# MVP ) - 22 Jan 2008 14:37 GMT Hi,
> In summary > [quoted text clipped - 5 lines] > > will different assembly names cause this error ? Do you have more than one assembly?
Remember that each type declared in a different assembly is a different type.
 Signature Ignacio Machin http://www.laceupsolutions.com Mobile & warehouse Solutions.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|