예제 #1
0
        /// <inheritdoc />
        public override bool Accept(Type targetRootType, Type targetMemberType, Type pastedDataType)
        {
            var sourceTypeDescriptor = TypeDescriptorFactory.Default.Find(pastedDataType);
            var targetTypeDescriptor = TypeDescriptorFactory.Default.Find(targetMemberType);

            // Can only paste a collection into another collection, or a dictionary into a dictionary
            if (sourceTypeDescriptor.Category == DescriptorCategory.Collection && targetTypeDescriptor.Category != DescriptorCategory.Collection ||
                sourceTypeDescriptor.Category == DescriptorCategory.Dictionary && targetTypeDescriptor.Category != DescriptorCategory.Dictionary)
            {
                return(false);
            }

            // Special case: KeyValuePair<,>
            if (targetTypeDescriptor.Category == DescriptorCategory.Dictionary && sourceTypeDescriptor.Category != DescriptorCategory.Dictionary)
            {
                // Key and Value types must be compatible
                var sourceKeyType   = sourceTypeDescriptor.Type.GetMember("Key").OfType <PropertyInfo>().FirstOrDefault()?.PropertyType;
                var sourceValueType = sourceTypeDescriptor.Type.GetMember("Value").OfType <PropertyInfo>().FirstOrDefault()?.PropertyType;
                return(sourceKeyType != null && TypeConverterHelper.CanConvert(sourceKeyType, ((DictionaryDescriptor)targetTypeDescriptor).KeyType) &&
                       sourceValueType != null && TypeConverterHelper.CanConvert(sourceValueType, ((DictionaryDescriptor)targetTypeDescriptor).ValueType));
            }
            // Types must be compatible
            var sourceType      = TypeDescriptorFactory.Default.Find(pastedDataType).GetInnerCollectionType();
            var destinationType = TypeDescriptorFactory.Default.Find(targetMemberType).GetInnerCollectionType();

            return(TypeConverterHelper.CanConvert(sourceType, destinationType));
        }
        internal bool IsEqual(object parentValue, object childValue, bool checkType = false)
        {
            if (checkType && parentValue != null && parentValue.GetType() != childValue.GetType())
            {
                if (TypeConverterHelper.CanConvert(childValue.GetType(), parentValue.ToString()))
                {
                    parentValue = Convert.ChangeType(parentValue, childValue.GetType());
                }
            }

            if ((childValue != null && childValue.Equals(parentValue)) || (parentValue == null && childValue == null))
            {
                return(true);
            }
            return(false);
        }