private void WriteMethodDefinitionSignature(IMethodDefinition method, string name)
        {
            bool isOperator = method.IsConversionOperator();

            if (!isOperator && !method.IsConstructor)
            {
                WriteAttributes(method.ReturnValueAttributes, true);

                if (method.Attributes.HasIsReadOnlyAttribute() && (LangVersion >= LangVersion8_0))
                {
                    WriteKeyword("readonly");
                }

                if (method.ReturnValueIsByRef)
                {
                    WriteKeyword("ref");

                    if (method.ReturnValueAttributes.HasIsReadOnlyAttribute())
                    {
                        WriteKeyword("readonly");
                    }
                }

                // We are ignoring custom modifiers right now, we might need to add them later.
                WriteTypeName(method.Type, method.ContainingType, method.ReturnValueAttributes);
            }

            if (method.IsExplicitInterfaceMethod() && _forCompilationIncludeGlobalprefix)
            {
                Write("global::");
            }

            WriteIdentifier(name);

            if (isOperator)
            {
                WriteSpace();

                WriteTypeName(method.Type, method.ContainingType);
            }

            Contract.Assert(!(method is IGenericMethodInstance), "Currently don't support generic method instances");
            if (method.IsGeneric)
            {
                WriteGenericParameters(method.GenericParameters);
            }

            WriteParameters(method.Parameters, method.ContainingType, extensionMethod: method.IsExtensionMethod(), acceptsExtraArguments: method.AcceptsExtraArguments);
            if (method.IsGeneric && !method.IsOverride() && !method.IsExplicitInterfaceMethod())
            {
                WriteGenericContraints(method.GenericParameters);
            }
        }
예제 #2
0
        private void WriteMethodName(IMethodDefinition method)
        {
            if (method.IsConstructor || method.IsStaticConstructor)
            {
                INamedEntity named = method.ContainingTypeDefinition.UnWrap() as INamedEntity;
                if (named != null)
                {
                    WriteIdentifier(named.Name.Value);
                    return;
                }
            }

            if (method.IsExplicitInterfaceMethod())
            {
                IMethodImplementation methodImplementation = method.GetMethodImplementation();
                object nullableAttributeArgument           = methodImplementation.GetExplicitInterfaceMethodNullableAttributeArgument(_metadataReaderCache);
                if (nullableAttributeArgument != null)
                {
                    WriteTypeName(methodImplementation.ImplementedMethod.ContainingType, noSpace: true, nullableAttributeArgument: nullableAttributeArgument);
                    WriteSymbol(".");
                    WriteIdentifier(methodImplementation.ImplementedMethod.Name);
                    return;
                }
            }

            WriteIdentifier(GetNormalizedMethodName(method.Name));
        }
        private void WriteMethodDefinitionSignature(IMethodDefinition method, string name)
        {
            bool isOperator = method.IsConversionOperator();

            if (!isOperator && !method.IsConstructor)
            {
                WriteAttributes(method.ReturnValueAttributes, true);
                // We are ignoring custom modifiers right now, we might need to add them later.
                WriteTypeName(method.Type, isDynamic: IsDynamic(method.ReturnValueAttributes));
            }

            WriteIdentifier(name);

            if (isOperator)
            {
                WriteSpace();
                WriteTypeName(method.Type);
            }

            Contract.Assert(!(method is IGenericMethodInstance), "Currently don't support generic method instances");
            if (method.IsGeneric)
            {
                WriteGenericParameters(method.GenericParameters);
            }

            WriteParameters(method.Parameters, extensionMethod: method.IsExtensionMethod(), acceptsExtraArguments: method.AcceptsExtraArguments);
            if (method.IsGeneric && !method.IsOverride() && !method.IsExplicitInterfaceMethod())
            {
                WriteGenericContraints(method.GenericParameters);
            }
        }
        private void WriteMethodDefinition(IMethodDefinition method)
        {
            if (method.IsPropertyOrEventAccessor())
            {
                return;
            }

            if (method.IsDestructor())
            {
                WriteDestructor(method);
                return;
            }

            string name = method.GetMethodName();

            WriteMethodPseudoCustomAttributes(method);

            WriteAttributes(method.Attributes);
            WriteAttributes(method.SecurityAttributes);

            if (!method.ContainingTypeDefinition.IsInterface)
            {
                if (!method.IsExplicitInterfaceMethod())
                {
                    WriteVisibility(method.Visibility);
                }
                WriteMethodModifiers(method);
            }
            WriteInterfaceMethodModifiers(method);
            WriteMethodDefinitionSignature(method, name);
            WriteMethodBody(method);
        }
        private void WriteMethodDefinitionSignature(IMethodDefinition method, string name)
        {
            bool isOperator = method.IsConversionOperator();

            if (!isOperator && !method.IsConstructor)
            {
                WriteAttributes(method.ReturnValueAttributes, true);
                // We are ignoring custom modifiers right now, we might need to add them later.
                WriteTypeName(method.Type, isDynamic: IsDynamic(method.ReturnValueAttributes));
            }

            WriteIdentifier(name);

            if (isOperator)
            {
                WriteSpace();
                WriteTypeName(method.Type);
            }

            Contract.Assert(!(method is IGenericMethodInstance), "Currently don't support generic method instances");
            if (method.IsGeneric)
                WriteGenericParameters(method.GenericParameters);

            WriteParameters(method.Parameters, extensionMethod: method.IsExtensionMethod(), acceptsExtraArguments: method.AcceptsExtraArguments);
            if (method.IsGeneric && !method.IsOverride() && !method.IsExplicitInterfaceMethod())
                WriteGenericContraints(method.GenericParameters);
        }
        private void WriteMethodDefinition(IMethodDefinition method)
        {
            if (method.IsPropertyOrEventAccessor())
                return;

            if (method.IsDestructor())
            {
                WriteDestructor(method);
                return;
            }

            string name = method.GetMethodName();

            WriteMethodPseudoCustomAttributes(method);

            WriteAttributes(method.Attributes);
            WriteAttributes(method.SecurityAttributes);

            if (!method.ContainingTypeDefinition.IsInterface)
            {
                if (!method.IsExplicitInterfaceMethod()) WriteVisibility(method.Visibility);
                WriteMethodModifiers(method);
            }
            WriteInterfaceMethodModifiers(method);
            WriteMethodDefinitionSignature(method, name);
            WriteMethodBody(method);
        }
예제 #7
0
        public override DifferenceType Diff(IDifferences differences, MemberMapping mapping)
        {
            ITypeDefinitionMember implMember     = mapping[0];
            ITypeDefinitionMember contractMember = mapping[1];
            IMethodDefinition     foundMethod;

            if (!(implMember == null && contractMember != null))
            {
                return(DifferenceType.Unknown);
            }

            // Nested types are handled separately.
            // @TODO: Events and Properties - should we consider these too (or rely on the fact that dropping one of these will also drop their accessors.)
            if (!(contractMember is IMethodDefinition || contractMember is IFieldDefinition))
            {
                return(DifferenceType.Unknown);
            }

            string incompatibeDifferenceMessage = $"Member '{contractMember.FullName()}' does not exist in the {Implementation} but it does exist in the {Contract}.";

            ITypeDefinition contractType = mapping.ContainingType[0];

            if (contractType != null)
            {
                IMethodDefinition contractMethod = contractMember as IMethodDefinition;
                if (contractMethod != null)
                {
                    // If the contract is a Explicit Interface method, we don't need to check if the method is in implementation since that will be caught by different rule.
                    if (contractMethod.IsExplicitInterfaceMethod())
                    {
                        return(DifferenceType.Unknown);
                    }



                    // It is valid to promote a member from a base type up so check to see if it member exits on a base type.
                    var lookForMethodInBaseResult = FindMatchingBase(contractType, contractMethod, out foundMethod);
                    if (lookForMethodInBaseResult == FindMethodResult.Found)
                    {
                        return(DifferenceType.Unknown);
                    }
                    if (lookForMethodInBaseResult == FindMethodResult.ReturnTypeChanged)
                    {
                        incompatibeDifferenceMessage += $" There does exist a member with return type '{foundMethod.GetReturnType().FullName()}' instead of '{contractMethod.GetReturnType().FullName()}'";
                    }
                }
            }

            differences.AddIncompatibleDifference(this, incompatibeDifferenceMessage);

            return(DifferenceType.Added);
        }
        private void WriteMethodDefinition(IMethodDefinition method)
        {
            if (method.IsPropertyOrEventAccessor())
            {
                return;
            }

            WriteMethodPseudoCustomAttributes(method);

            WriteAttributes(method.Attributes);
            WriteAttributes(method.SecurityAttributes);
            WriteAttributes(method.ReturnValueAttributes, prefix: "return");

            if (method.IsDestructor())
            {
                // If platformNotSupportedExceptionMessage is != null we're generating a dummy assembly which means we don't need a destructor at all.
                if (_platformNotSupportedExceptionMessage == null)
                {
                    WriteDestructor(method);
                }

                return;
            }

            var writeVisibility = true;

            if (method.ContainingTypeDefinition.IsInterface)
            {
                writeVisibility = false;
            }

            if (method.IsExplicitInterfaceMethod() || method.IsStaticConstructor)
            {
                writeVisibility = false;
            }

            if (writeVisibility)
            {
                WriteVisibility(method.Visibility);
            }

            WriteMethodModifiers(method);

            WriteInterfaceMethodModifiers(method);
            WriteMethodDefinitionSignature(method);
            WriteMethodBody(method);
        }
예제 #9
0
        private void WriteEventDefinition(IEventDefinition evnt)
        {
            // Adder and Remover modifiers should be same.
            IMethodDefinition accessor = evnt.Accessors.First().ResolvedMethod;

            if (!evnt.ContainingTypeDefinition.IsInterface)
            {
                WriteAttributes(evnt.Attributes);
                if (!accessor.IsExplicitInterfaceMethod())
                {
                    WriteVisibility(evnt.Visibility);
                }
                WriteMethodModifiers(accessor);
            }

            if (evnt.GetHiddenBaseEvent(_filter) != Dummy.Event)
            {
                WriteKeyword("new");
            }

            if (accessor.Attributes.HasIsReadOnlyAttribute() && (LangVersion >= LangVersion8_0))
            {
                WriteKeyword("readonly");
            }

            WriteKeyword("event");
            WriteTypeName(evnt.Type, evnt.Attributes);
            WriteIdentifier(evnt.Name);

            if (_forCompilation && !evnt.IsAbstract())
            {
                WriteSpace();
                WriteSymbol("{", addSpace: true);
                WriteEventBody("add");
                WriteEventBody("remove");
                WriteSymbol("}");
            }
            else
            {
                WriteSymbol(";");
            }
        }
예제 #10
0
        private void WriteMethodDefinition(IMethodDefinition method)
        {
            if (method.IsPropertyOrEventAccessor())
            {
                return;
            }

            WriteMethodPseudoCustomAttributes(method);

            WriteAttributes(method.Attributes);
            WriteAttributes(method.SecurityAttributes);

            if (method.IsDestructor())
            {
                // If platformNotSupportedExceptionMessage is != null we're generating a dummy assembly which means we don't need a destructor at all.
                if (_platformNotSupportedExceptionMessage == null)
                {
                    WriteDestructor(method);
                }

                return;
            }

            string name = method.GetMethodName();

            if (!method.ContainingTypeDefinition.IsInterface)
            {
                if (!method.IsExplicitInterfaceMethod())
                {
                    WriteVisibility(method.Visibility);
                }
                WriteMethodModifiers(method);
            }
            WriteInterfaceMethodModifiers(method);
            WriteMethodDefinitionSignature(method, name);
            WriteMethodBody(method);
        }
예제 #11
0
        private void WritePropertyDefinition(IPropertyDefinition property)
        {
            bool isInterfaceProp       = property.ContainingTypeDefinition.IsInterface;
            IMethodDefinition accessor = null;
            IMethodDefinition getter   = null;
            IMethodDefinition setter   = null;

            if (property.Getter != null)
            {
                getter = property.Getter.ResolvedMethod;
                if (!_filter.Include(getter))
                {
                    getter = null;
                }
                accessor = getter;
            }

            if (property.Setter != null)
            {
                setter = property.Setter.ResolvedMethod;
                if (!_filter.Include(setter))
                {
                    setter = null;
                }
                if (accessor == null)
                {
                    accessor = setter;
                }
            }

            if (accessor == null)
            {
                return;
            }

            bool isIndexer = accessor.ParameterCount > (accessor == setter ? 1 : 0);

            if (isIndexer)
            {
                string id    = property.Name.Value;
                int    index = id.LastIndexOf(".");
                if (index >= 0)
                {
                    id = id.Substring(index + 1);
                }

                if (id != "Item")
                {
                    WriteFakeAttribute("System.Runtime.CompilerServices.IndexerName", "\"" + id + "\"");
                }
            }

            WriteAttributes(property.Attributes);

            if (!isInterfaceProp)
            {
                if (!accessor.IsExplicitInterfaceMethod())
                {
                    WriteVisibility(property.Visibility);
                }

                // Getter and Setter modifiers should be the same
                WriteMethodModifiers(accessor);
            }
            if (property.GetHiddenBaseProperty(_filter) != Dummy.Property)
            {
                WriteKeyword("new");
            }

            if (property.ReturnValueIsByRef)
            {
                WriteKeyword("ref");
            }

            WriteTypeName(property.Type);

            if (isIndexer)
            {
                int index = property.Name.Value.LastIndexOf(".");
                if (index >= 0)
                {
                    WriteIdentifier(property.Name.Value.Substring(0, index + 1) + "this", false); // +1 to include the '.'
                }
                else
                {
                    WriteIdentifier("this", false);
                }

                var parameters = new List <IParameterDefinition>(accessor.Parameters);
                if (accessor == setter) // If setter remove value parameter.
                {
                    parameters.RemoveAt(parameters.Count - 1);
                }
                WriteParameters(parameters, property.ContainingType, true);
            }
            else
            {
                WriteIdentifier(property.Name);
            }
            WriteSpace();
            WriteSymbol("{");

            //get
            if (getter != null)
            {
                WriteAccessorDefinition(property, getter, "get");
            }
            //set
            if (setter != null)
            {
                WriteAccessorDefinition(property, setter, "set");
            }
            WriteSpace();
            WriteSymbol("}");
        }
        private void WritePropertyDefinition(IPropertyDefinition property)
        {
            bool isInterfaceProp       = property.ContainingTypeDefinition.IsInterface;
            IMethodDefinition accessor = null;
            IMethodDefinition getter   = null;
            IMethodDefinition setter   = null;

            if (property.Getter != null)
            {
                getter = property.Getter.ResolvedMethod;
                if (!_filter.Include(getter))
                {
                    getter = null;
                }
                accessor = getter;
            }

            if (property.Setter != null)
            {
                setter = property.Setter.ResolvedMethod;
                if (!_filter.Include(setter))
                {
                    setter = null;
                }
                if (accessor == null)
                {
                    accessor = setter;
                }
            }

            if (accessor == null)
            {
                return;
            }

            bool isIndexer = accessor.ParameterCount > (accessor == setter ? 1 : 0);

            if (isIndexer)
            {
                string id    = property.Name.Value;
                int    index = id.LastIndexOf(".");
                if (index >= 0)
                {
                    id = id.Substring(index + 1);
                }

                if (id != "Item")
                {
                    WriteFakeAttribute("System.Runtime.CompilerServices.IndexerName", "\"" + id + "\"");
                }
            }

            WriteAttributes(property.Attributes);

            // We need to preserve nullable custom attributes which are preserved by the compiler as param on the value parameter and return attributes.
            if (getter != null)
            {
                WriteAttributes(getter.ReturnValueAttributes);
            }
            if (setter != null)
            {
                WriteAttributes(setter.Parameters.Last().Attributes);
            }

            if (!isInterfaceProp)
            {
                if (!accessor.IsExplicitInterfaceMethod())
                {
                    WriteVisibility(property.Visibility);
                }

                // Getter and Setter modifiers should be the same
                WriteMethodModifiers(accessor);
            }
            if (property.GetHiddenBaseProperty(_filter) != Dummy.Property)
            {
                WriteKeyword("new");
            }

            bool getterHasIsReadOnlyAttribute = (getter?.Attributes.HasIsReadOnlyAttribute()).GetValueOrDefault();
            bool setterHasIsReadOnlyAttribute = (setter?.Attributes.HasIsReadOnlyAttribute()).GetValueOrDefault();

            // The readonly modifier is applied on the property itself if:
            //  * It has both a getter and a setter and both have IsReadOnlyAttribute
            //  * It only has a getter or a setter and it has IsReadOnlyAttribute
            // Otherwise, the attribute is applied directly on the getter/setter it exists for
            bool allAccessorsHaveIsReadOnlyAttribute = (getterHasIsReadOnlyAttribute && setterHasIsReadOnlyAttribute) ||
                                                       (getterHasIsReadOnlyAttribute && (setter is null)) ||
                                                       (setterHasIsReadOnlyAttribute && (getter is null));

            if (allAccessorsHaveIsReadOnlyAttribute && (LangVersion >= LangVersion8_0))
            {
                WriteKeyword("readonly");
            }

            if (property.ReturnValueIsByRef)
            {
                WriteKeyword("ref");

                if (property.Attributes.HasIsReadOnlyAttribute())
                {
                    WriteKeyword("readonly");
                }
            }

            WriteTypeName(property.Type, attributes: property.Attributes);

            if (property.IsExplicitInterfaceProperty() && _forCompilationIncludeGlobalprefix)
            {
                Write("global::");
            }

            WritePropertyName(property, accessor, accessor == setter, isIndexer);
            WriteSpace();
            WriteSymbol("{");

            //get
            if (getter != null)
            {
                bool isReadOnly = getterHasIsReadOnlyAttribute && !allAccessorsHaveIsReadOnlyAttribute;
                WriteAccessorDefinition(property, getter, "get", isReadOnly);
            }
            //set
            if (setter != null)
            {
                bool isReadOnly = setterHasIsReadOnlyAttribute && !allAccessorsHaveIsReadOnlyAttribute;
                WriteAccessorDefinition(property, setter, "set", isReadOnly);
            }
            WriteSpace();
            WriteSymbol("}");
        }