Casting PropertyInfo.getValue () zu einem Typ, wenn der Typ nicht nullbar istC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Casting PropertyInfo.getValue () zu einem Typ, wenn der Typ nicht nullbar ist

Post by Anonymous »

Betrachten Sie diesen Beispielcode: < /p>

Code: Select all

var properties = c.GetType().GetProperties();
// note that c is a System.Windows.Forms.Control

foreach( var p in properties )
{
if (p.PropertyType.Name.Contains("Color") )
{
var colour_value = p.GetValue(c,null);

// I need to cast colour_value to a Color
}
}
Ich muss die Ausgabe von p.getValue (c, null) auf ein System gießen.Drawing.Color . Ich habe andere Fragen gesehen, in denen die Leute in einer ähnlichen Situation entweder vorgeschlagen haben: < /p>

Code: Select all

(Color) p.GetValue(c, null)
< /code>
oder < /p>
p.GetValue(c, null) as Color
Aber diese funktionieren nicht, da Color ein nicht nullbarer Werttyp ist. Es führt daher den Compiler -Fehler CS0077 aus.
Gibt es eine Möglichkeit, dies zu tun, bitte?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post
  • Warum ist IEnumerator.Current nicht nullbar?
    by Anonymous » » in C#
    0 Replies
    10 Views
    Last post by Anonymous
  • SQLite - Typ Casting in Python
    by Anonymous » » in Python
    0 Replies
    0 Views
    Last post by Anonymous
  • Welchen Sinn hat es, einen Referenztyp nullbar zu machen?
    by Guest » » in C#
    0 Replies
    22 Views
    Last post by Guest
  • Casting von größerem Datentyp (Struktur) bis kleiner
    by Anonymous » » in C++
    0 Replies
    3 Views
    Last post by Anonymous
  • Wie Deserialize [jSonextsionData] Dictionary mit Casting
    by Anonymous » » in C#
    0 Replies
    2 Views
    Last post by Anonymous