Exemplo n.º 1
0
        static PropertySyncHelper()
        {
            // Create the dictionary cache that maps a PropertySyncHandler's handled type
            // to the type of the PropertySyncHandler itself

            var typeFilterCreator = new TypeFilterCreator
            {
                IsClass = true, IsAbstract = false, Attributes = new Type[] { typeof(PropertySyncHandlerAttribute) }
            };

            var typeFilter = typeFilterCreator.GetFilter();

            foreach (var type in TypeHelper.AllTypes().Where(typeFilter))
            {
                // Look for classes that inherit the PropertySyncHandlerAttribute
                var attribs = type.GetCustomAttributes(typeof(PropertySyncHandlerAttribute), true);
                if (attribs.Length < 1)
                {
                    continue;
                }

                if (attribs.Length > 1)
                {
                    const string errmsg = "Multiple PropertySyncHandlerAttributes found on type `{0}`";
                    if (log.IsErrorEnabled)
                    {
                        log.ErrorFormat(errmsg, type);
                    }
                    Debug.Fail(string.Format(errmsg, type));
                    throw new TypeLoadException(string.Format(errmsg, type));
                }

                // Grab the attribute so we can find out what type this class handles
                var attrib = (PropertySyncHandlerAttribute)attribs[0];

                // Make sure the handler doesn't already exist
                if (_propertySyncTypes.ContainsKey(attrib.HandledType))
                {
                    const string errmsg =
                        "Duplicate PropertySync implementations for type `{0}`. Implementations: `{1}` and `{2}`.";
                    var existingPST = _propertySyncTypes[attrib.HandledType];
                    if (log.IsErrorEnabled)
                    {
                        log.ErrorFormat(errmsg, attrib.HandledType, existingPST, type);
                    }
                    Debug.Fail(string.Format(errmsg, attrib.HandledType, existingPST, type));
                    continue;
                }

                // Store the handled type
                _propertySyncTypes.Add(attrib.HandledType, type);

                // If the type can be made nullable, also store the nullable type
                if (attrib.HandledType.IsValueType && !attrib.HandledType.IsGenericType)
                {
                    try
                    {
                        var nullableType = typeof(Nullable <>).MakeGenericType(attrib.HandledType);

                        // Make sure the key doesn't already exist
                        if (!_propertySyncTypes.ContainsKey(nullableType))
                        {
                            var psType = typeof(PropertySyncNullable <>).MakeGenericType(attrib.HandledType);
                            _propertySyncTypes.Add(nullableType, psType);
                        }
                    }
                    catch (Exception ex)
                    {
                        const string errmsg = "Failed to create nullable type from `{0}`. Reason: {1}";
                        if (log.IsErrorEnabled)
                        {
                            log.ErrorFormat(errmsg, attrib.HandledType, ex);
                        }
                        Debug.Fail(string.Format(errmsg, attrib.HandledType, ex));
                    }
                }
            }
        }