Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

946
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda