Is the "Possible System.NullReferenceException" for PropertyType/FieldType members correct?

[5.1.1704]


public class Foo
{
public void Bar(System.Reflection.MemberInfo memberInfo)
{
var propertyInfo = memberInfo as System.Reflection.PropertyInfo;
var fieldInfo = memberInfo as System.Reflection.FieldInfo;
string name;
if ( propertyInfo != null )
name = propertyInfo.PropertyType.Name; //Possible 'System.NullReferenceException'
else if (fieldInfo != null )
name = fieldInfo.FieldType.Name; //Possible 'System.NullReferenceException'

}
}

Can these ever return null? If not, maybe use the annotate notnull-magic for these 2 members?

1 comment
Comment actions Permalink

Hello,

System.Reflection.PropertyInfo.PropertyType is an abstract property and its
implementation in System.Reflection.RuntimePropertyInfo (in .NET Framework
3.5) can return null. Based on this information ReSharper considers that
PropertyType can be null as well. The same happens with System.Reflection.FieldInfo.FieldType.
Thank you!

Andrey Serebryansky
Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"



public class Foo
{
public void Bar(System.Reflection.MemberInfo memberInfo)
{
var propertyInfo = memberInfo as System.Reflection.PropertyInfo;
var fieldInfo = memberInfo as System.Reflection.FieldInfo;
string name;
if ( propertyInfo != null )
name = propertyInfo.PropertyType.Name; //Possible
'System.NullReferenceException'
else if (fieldInfo != null )
name = fieldInfo.FieldType.Name; //Possible
'System.NullReferenceException'
}
}
Can these ever return null? If not, maybe use the annotate
notnull-magic for these 2 members?

---
Original message URL:
http://www.jetbrains.net/devnet/message/5265016#5265016



0

Please sign in to leave a comment.