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