private void InnerProcess(PropertyDefinition property)
    {
        var localValidationFlags = validationFlags;

        if (!property.PropertyType.IsRefType())
        {
            return;
        }

        var attribute = property.DeclaringType.GetNullGuardAttribute();

        if (attribute != null)
        {
            localValidationFlags = (ValidationFlags)attribute.ConstructorArguments[0].Value;
        }

        if (!localValidationFlags.HasFlag(ValidationFlags.Properties))
        {
            return;
        }

        if (property.AllowsNull())
        {
            return;
        }

        if (property.GetMethod != null && property.GetMethod.Body != null)
        {
            var getBody = property.GetMethod.Body;
            getBody.SimplifyMacros();

            if ((localValidationFlags.HasFlag(ValidationFlags.NonPublic) || (property.GetMethod.IsPublic && property.DeclaringType.IsPublic)) &&
                !property.GetMethod.MethodReturnType.AllowsNull()
                )
            {
                InjectPropertyGetterGuard(getBody, property.PropertyType, String.Format(CultureInfo.InvariantCulture, STR_ReturnValueOfPropertyIsNull, property.FullName));
            }

            getBody.InitLocals = true;
            getBody.OptimizeMacros();
        }

        if (property.SetMethod != null && property.SetMethod.Body != null)
        {
            var setBody = property.SetMethod.Body;
            setBody.SimplifyMacros();

            if (localValidationFlags.HasFlag(ValidationFlags.NonPublic) || (property.SetMethod.IsPublic && property.DeclaringType.IsPublic))
            {
                InjectPropertySetterGuard(setBody, property.SetMethod.Parameters[0], String.Format(CultureInfo.InvariantCulture, STR_CannotSetTheValueOfPropertyToNull, property.FullName));
            }

            setBody.InitLocals = true;
            setBody.OptimizeMacros();
        }
    }
Example #2
0
    void InnerProcess(PropertyDefinition property)
    {
        var localValidationFlags = validationFlags;

        if (!property.PropertyType.IsRefType())
        {
            return;
        }

        var attribute = property.DeclaringType.GetNullGuardAttribute();

        if (attribute != null)
        {
            localValidationFlags = (ValidationFlags)attribute.ConstructorArguments[0].Value;
        }

        if (!localValidationFlags.HasFlag(ValidationFlags.Properties))
        {
            return;
        }

        if (property.AllowsNull())
        {
            return;
        }

        if (property.GetMethod?.Body != null)
        {
            var getMethod = property.GetMethod;

            var doc = getMethod.DebugInformation.SequencePoints.FirstOrDefault()?.Document;

            getMethod.Body.SimplifyMacros();

            if ((localValidationFlags.HasFlag(ValidationFlags.NonPublic) ||
                 property.GetMethod.IsPublic &&
                 property.DeclaringType.IsPublicOrNestedPublic()) &&
                !property.GetMethod.MethodReturnType.AllowsNull()
                )
            {
                InjectPropertyGetterGuard(getMethod, doc, property);
            }

            getMethod.Body.InitLocals = true;
            getMethod.Body.OptimizeMacros();
        }

        if (property.SetMethod?.Body != null)
        {
            var setBody = property.SetMethod.Body;

            var doc = property.SetMethod.DebugInformation.SequencePoints.FirstOrDefault()?.Document;

            setBody.SimplifyMacros();

            if (localValidationFlags.HasFlag(ValidationFlags.NonPublic) ||
                property.SetMethod.IsPublic &&
                property.DeclaringType.IsPublicOrNestedPublic())
            {
                InjectPropertySetterGuard(property.SetMethod, doc, property);
            }

            setBody.InitLocals = true;
            setBody.OptimizeMacros();
        }
    }
Example #3
0
    void InnerProcess(PropertyDefinition property)
    {
        var localValidationFlags = ValidationFlags;

        if (!property.PropertyType.IsRefType())
        {
            return;
        }

        var attribute = property.DeclaringType.GetNullGuardAttribute();

        if (attribute != null)
        {
            localValidationFlags = (ValidationFlags)attribute.ConstructorArguments[0].Value;
        }

        if (!localValidationFlags.HasFlag(ValidationFlags.Properties))
        {
            return;
        }

        if (property.AllowsNull(explicitMode))
        {
            return;
        }

        var getMethod = property.GetMethod;
        var getBody   = getMethod?.Body;

        if (getBody != null)
        {
            getMethod.Body.SimplifyMacros();

            if ((localValidationFlags.HasFlag(ValidationFlags.NonPublic) ||
                 (getMethod.IsPublic && property.DeclaringType.IsPublicOrNestedPublic()) ||
                 getMethod.IsOverrideOrImplementationOfPublicMember()) &&
                !getMethod.MethodReturnType.ImplicitAllowsNull())
            {
                InjectPropertyGetterGuard(getMethod, property);
            }

            getMethod.UpdateDebugInfo();
            getBody.InitLocals = true;
            getBody.OptimizeMacros();
        }

        var setMethod = property.SetMethod;
        var setBody   = setMethod?.Body;

        if (setBody != null)
        {
            setBody.SimplifyMacros();

            if (localValidationFlags.HasFlag(ValidationFlags.NonPublic) ||
                (setMethod.IsPublic && property.DeclaringType.IsPublicOrNestedPublic()) ||
                setMethod.IsOverrideOrImplementationOfPublicMember())
            {
                InjectPropertySetterGuard(setMethod, property);
            }

            setMethod.UpdateDebugInfo();
            setBody.InitLocals = true;
            setBody.OptimizeMacros();
        }
    }