示例#1
0
        internal static GenerateResults Generate(Type baseType, SortedSet <string> namespaces,
                                                 NameGenerator generator, MethodInformationBuilder informationBuilder,
                                                 bool isMake, bool hasEvents)
        {
            var requiresObsoleteSuppression = false;

            var generatedProperties = new List <string>();

            foreach (var property in baseType.GetMockableProperties(generator))
            {
                var baseProperty = property.Value;

                namespaces.Add(baseProperty.PropertyType.Namespace);
                var indexers          = baseProperty.GetIndexParameters();
                var propertyMethod    = baseProperty.GetDefaultMethod();
                var methodInformation = informationBuilder.Build(new MockableResult <MethodInfo>(
                                                                     propertyMethod, RequiresExplicitInterfaceImplementation.No));

                if (propertyMethod.IsPublic)
                {
                    var @override = methodInformation.DescriptionWithOverride.Contains("override", StringComparison.Ordinal) ? "override " : string.Empty;
                    requiresObsoleteSuppression = PropertiesGenerator.HandlePublicProperty(
                        namespaces, isMake, hasEvents, requiresObsoleteSuppression, generatedProperties, property, baseProperty, indexers, @override);
                }
                else if (!propertyMethod.IsPrivate && propertyMethod.IsAbstract)
                {
                    requiresObsoleteSuppression = PropertiesGenerator.HandleNonPrivateAbstractProperty(
                        namespaces, requiresObsoleteSuppression, generatedProperties, property, baseProperty, indexers, propertyMethod);
                }
            }

            return(new GenerateResults(string.Join(Environment.NewLine, generatedProperties),
                                       requiresObsoleteSuppression, false));
        }
        private static bool HandlePublicProperty(SortedSet <string> namespaces, bool isMake, bool hasEvents,
                                                 bool requiresObsoleteSuppression, List <string> generatedProperties, PropertyMockableResult property,
                                                 PropertyInfo baseProperty, ParameterInfo[] indexers, string @override)
        {
            var propertyImplementations = new List <string>();
            var parameter = property.Accessors == PropertyAccessors.Get || property.Accessors == PropertyAccessors.GetAndSet ?
                            baseProperty.GetGetMethod().ReturnParameter :
                            baseProperty.GetSetMethod().GetParameters()[0];

            if (property.Accessors == PropertyAccessors.Get || property.Accessors == PropertyAccessors.GetAndSet)
            {
                PropertiesGenerator.HandlePublicGetter(namespaces, isMake, hasEvents, baseProperty, propertyImplementations);
            }

            if (property.Accessors == PropertyAccessors.Set || property.Accessors == PropertyAccessors.GetAndSet)
            {
                PropertiesGenerator.HandlePublicSetter(isMake, hasEvents, baseProperty, propertyImplementations);
            }

            var visibility = property.RequiresExplicitInterfaceImplementation == RequiresExplicitInterfaceImplementation.Yes ?
                             string.Empty : CodeTemplates.Public;
            var explicitInterfaceName = property.RequiresExplicitInterfaceImplementation == RequiresExplicitInterfaceImplementation.Yes ?
                                        $"{property.Value.DeclaringType.GetFullName(namespaces)}." : string.Empty;

            if (indexers.Length > 0)
            {
                var parameters = string.Join(", ",
                                             from indexer in indexers
                                             let _ = namespaces.Add(indexer.ParameterType.Namespace)
                                                     select $"{indexer.ParameterType.Name} {indexer.Name}");

                // Indexer
                generatedProperties.Add(PropertyTemplates.GetPropertyIndexer(
                                            $"{@override}{baseProperty.PropertyType.GetFullName(namespaces, parameter)}", parameters,
                                            string.Join(Environment.NewLine, propertyImplementations), visibility, explicitInterfaceName));
            }
            else
            {
                // Normal
                generatedProperties.Add(PropertyTemplates.GetProperty(
                                            $"{@override}{baseProperty.PropertyType.GetFullName(namespaces, parameter)}", baseProperty.Name,
                                            string.Join(Environment.NewLine, propertyImplementations), visibility, explicitInterfaceName));
            }

            requiresObsoleteSuppression |= baseProperty.GetCustomAttribute <ObsoleteAttribute>() != null;
            return(requiresObsoleteSuppression);
        }