Browse Source

simplified check for reference types based on Daniel's advice

pull/1/head
Siegfried Pammer 15 years ago
parent
commit
428f9a5f21
  1. 3
      src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/InsertCtorDialog.xaml.cs
  2. 23
      src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/PropertyOrFieldWrapper.cs

3
src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/InsertCtorDialog.xaml.cs

@ -53,7 +53,8 @@ namespace SharpRefactoring.Gui
foreach (PropertyOrFieldWrapper w in filtered) { foreach (PropertyOrFieldWrapper w in filtered) {
if (w.AddCheckForNull) { if (w.AddCheckForNull) {
if (w.Type.IsReferenceType == true || PropertyOrFieldWrapper.IsGenericReferenceType(w.Type)) // true = reference, null = generic or unknown
if (w.Type.IsReferenceType != false)
block.AddChild( block.AddChild(
new IfElseStatement( new IfElseStatement(
new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.Equality, new PrimitiveExpression(null)), new BinaryOperatorExpression(new IdentifierExpression(w.ParameterName), BinaryOperatorType.Equality, new PrimitiveExpression(null)),

23
src/AddIns/Misc/SharpRefactoring/Project/Src/Gui/PropertyOrFieldWrapper.cs

@ -26,7 +26,7 @@ namespace SharpRefactoring.Gui
public PropertyOrFieldWrapper(IMember member) public PropertyOrFieldWrapper(IMember member)
{ {
if (member == null || member.ReturnType == null) if (member == null || member.ReturnType == null)
throw new ArgumentNullException("field"); throw new ArgumentNullException("member");
if (!(member is IField || member is IProperty)) if (!(member is IField || member is IProperty))
throw new ArgumentException("member must be IField or IProperty"); throw new ArgumentException("member must be IField or IProperty");
@ -39,7 +39,7 @@ namespace SharpRefactoring.Gui
string parameterName; string parameterName;
public string ParameterName { public string ParameterName {
get { get {
if (parameterName == null) if (parameterName == null)
parameterName = ToParameterName(this.MemberName); parameterName = ToParameterName(this.MemberName);
return parameterName; return parameterName;
@ -58,27 +58,18 @@ namespace SharpRefactoring.Gui
public bool IsNullable { public bool IsNullable {
get { get {
return member.ReturnType.IsReferenceType == true // true = reference, null = generic or unknown
|| member.ReturnType.IsConstructedReturnType && member.ReturnType.Name == "Nullable" return member.ReturnType.IsReferenceType != false
|| IsGenericReferenceType(member.ReturnType); || (member.ReturnType.IsConstructedReturnType && member.ReturnType.Name == "Nullable");
} }
} }
public static bool IsGenericReferenceType(IReturnType type)
{
if (type == null || !type.IsGenericReturnType)
return false;
ITypeParameter param = type.CastToGenericReturnType().TypeParameter;
return param.HasReferenceTypeConstraint
|| param.Constraints.Any(constr => constr.IsReferenceType == true);
}
public bool HasRange { public bool HasRange {
get { get {
return IsTypeWithRange(member.ReturnType) || return IsTypeWithRange(member.ReturnType) ||
// IsConstructedReturnType handles Nullable types // IsConstructedReturnType handles Nullable types
(member.ReturnType.IsConstructedReturnType && (member.ReturnType.IsConstructedReturnType && member.ReturnType.Name == "Nullable"
IsTypeWithRange(member.ReturnType.CastToConstructedReturnType().TypeArguments.First())); && IsTypeWithRange(member.ReturnType.CastToConstructedReturnType().TypeArguments.First()));
} }
} }

Loading…
Cancel
Save