En C#, ¿cómo puedo saber si una propiedad es estática? (. Net CF 2.0)
FieldInfo tiene un miembro isStatic, pero PropertyInfo no. Asumo que estoy pasando por alto lo que necesito.
Type type = someObject.GetType();
foreach (PropertyInfo pi in type.GetProperties())
{
// umm... Not sure how to tell if this property is static
}
23
Author: CrashCodes, 2008-12-24
4 answers
Para determinar si una propiedad es estática, debe obtener el MethodInfo para el accessor get o set, llamando al método GetGetMethod o GetSetMethod, y examinar su propiedad isStatic.
Http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx
38
Author: Steven Behnke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-12-24 19:44:58
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-12-24 19:44:58
¿Por qué no usar
type.GetProperties(BindingFlags.Static)
11
Author: ctacke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-27 14:07:50
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-27 14:07:50
Como una solución rápida y simple a la pregunta planteada, puede usar esto:
property.GetAccessors(true)[0].IsStatic;
3
Author: relatively_random,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-03 12:44:00
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-03 12:44:00
Mejor solución
public static class PropertyInfoExtensions
{
public static bool IsStatic(this PropertyInfo source, bool nonPublic = false)
=> source.GetAccessors(nonPublic).Any(x => x.IsStatic);
}
Uso:
property.IsStatic()
2
Author: furier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-20 11:49:46
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-20 11:49:46