Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

933
Vistas
C#: Unable to cast object of type 'System.Int64' to type 'System.Int32'

I have code as follows:

Dictionary<object, object> dict = ...
Color = (int)dict.GetValue("color");

When I convert the Color to an int, I get the following exception:

System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.Int32'.

I am not sure why I can't just cast from a long to an int. I know for a fact that the value is less than 0xFFFFFF (24 bits) since it is a color.

I tried using unchecked but that didn't help either.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You must first unbox the value as the dictionary's value type is object.

Dictionary<object, object> dict = ...
Color = (int)(long)dict.GetValue("color");
over 4 years ago · Santiago Trujillo Denunciar

0

If you don't know the original type, the following idiom may be more convenient.

public T Get<T>(string key)
{
    return (T) Convert.ChangeType(_dict[key], typeof(T), CultureInfo.InvariantCulture);
}
over 4 years ago · Santiago Trujillo Denunciar

0

cannot be tracked because another instance with the same key value for is already being tracked

find solution

  context.Patrol.AddRange(model);  // error

entity.Property(e => e.Id)
                    .HasColumnName("ID")
                    .ValueGeneratedNever(); // old

entity.Property(e => e.Id)
                    .HasColumnName("ID"); //remove ValueGeneratedNever()
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda