当前位置: 首页>>代码示例>>C#>>正文


C# IComparable.GetType方法代码示例

本文整理汇总了C#中IComparable.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# IComparable.GetType方法的具体用法?C# IComparable.GetType怎么用?C# IComparable.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IComparable的用法示例。


在下文中一共展示了IComparable.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Compare

		internal static int Compare (IComparable o1, IComparable o2, bool caseSensitive)
		{
			//TODO: turn this "conversion pipeline" into something nicer

			try {
				if (o1 is string && Numeric.IsNumeric (o2))
					o1 = (IComparable) Convert.ChangeType (o1, o2.GetType ());
				else if (o2 is string && Numeric.IsNumeric (o1))
					o2 = (IComparable) Convert.ChangeType (o2, o1.GetType ());
				else if (o1 is string && o2 is Guid)
					o2 = o2.ToString ();
				else if (o2 is string && o1 is Guid)
					o1 = o1.ToString ();
			} catch (FormatException) {
				throw new EvaluateException (String.Format ("Cannot perform compare operation on {0} and {1}.", o1.GetType(), o2.GetType()));
			}

			if (o1 is string && o2 is string && !caseSensitive) {
				o1 = ((string)o1).ToLower();
				o2 = ((string)o2).ToLower();
			}
			
			if (o1.GetType () != o2.GetType ())
				o2 = (IComparable)Convert.ChangeType (o2, o1.GetType ());

			return o1.CompareTo (o2);
		}
开发者ID:jjenki11,项目名称:blaze-chem-rendering,代码行数:27,代码来源:Comparison.cs

示例2: ArithmeticCriteria

        /// <summary>
        /// Initializes a new instance of the <see cref="ArithmeticCriteria"/> class.
        /// </summary>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="value">The value.</param>
        /// <param name="operand">The operand.</param>
        public ArithmeticCriteria(string fieldName, IComparable value, ArithmeticOperandEnum operand)
            : base(fieldName)
        {
            if (value == null)
            {
                ThrowHelper.ThrowArgumentNullException("value");
            }

            Type valueType = value.GetType();
            if (valueType.IsGenericType && valueType.GetGenericTypeDefinition().Equals(typeof(EntityBaseGenericId<>)))
            {
                dynamic dynValue = value;
                if (dynValue.Id == null)
                {
                    ThrowHelper.ThrowArgumentException(String.Format("Provided entity has not got identifier. Entity type: '{0}'.", valueType.FullName), "value");
                }
                if (fieldName.ToLower().Equals("id"))
                {
                    this.FieldName = fieldName;
                }
                else
                {
                    this.FieldName = fieldName.ToLower().EndsWith(".id") ? fieldName : String.Format("{0}.id", fieldName);
                }
                this.mValue = dynValue.Id;
            }
            else
            {
                this.mValue = value;
            }
            this.mOperand = operand;
        }
开发者ID:JZO001,项目名称:Forge,代码行数:38,代码来源:ArithmeticCriteria.cs

示例3: BetweenValidator

        /// <summary>
        /// Initializes a new instance of the <see cref="BetweenValidator"/> class.
        /// </summary>
        /// <param name="from">From.</param>
        /// <param name="to">The automatic.</param>
        public BetweenValidator(IComparable @from, IComparable to)
        {
            if (@from == null)
            {
                throw new ArgumentNullException("from");
            }

            if (to == null)
            {
                throw new ArgumentNullException("to");
            }

            if (!to.GetType().IsInstanceOfType(@from))
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "'To' value should be type of '{0}'", @from.GetType().FullName));
            }

            if (to.CompareTo(@from) == -1)
            {
                throw new ArgumentOutOfRangeException("to", "'To' should be larger than 'from'.");
            }

            m_From = @from;
            m_To = to;

            m_ValidatorProperties = new ValidatorProperties
                                        {
                                            { Constants.ValidationMessageParameterNames.FROM_VALUE, FromValue },
                                            { Constants.ValidationMessageParameterNames.TO_VALUE, ToValue }
                                        };
        }
开发者ID:QuickOrBeDead,项目名称:Labo.Validation,代码行数:36,代码来源:BetweenValidator.cs

示例4: ValueDescription

 /// <summary>
 /// Set ImplicitDefault to the default value of <paramref name="explicitDefault"/>'s type,
 /// and ExplicitDefault to <paramref name="explicitDefault"/>.
 /// </summary>
 /// <param name="explicitDefault"></param>
 /// <param name="storeInBase"></param>
 /// <param name="nativeToString"></param>
 internal ValueDescription(IComparable explicitDefault, bool storeInBase = true, ValueNativeToString nativeToString = null)
 {
     ImplicitDefault = GetImplicitDefault(explicitDefault.GetType());
     ExplicitDefault = explicitDefault;
     DefaultsDiffer = (ImplicitDefault.CompareTo(ExplicitDefault) != 0);
     StoreInBase = storeInBase;
     NativeToString = nativeToString;
 }
开发者ID:hultqvist,项目名称:Npgsql2,代码行数:15,代码来源:NpgsqlConnectionStringBuilder.cs

示例5: Compare

		internal static int Compare (IComparable o1, IComparable o2, bool caseSensitive)
		{
			//TODO: turn this "conversion pipeline" into something nicer

			try {
				if (o1 is string && Numeric.IsNumeric (o2))
					o1 = (IComparable) Convert.ChangeType (o1, o2.GetType ());
				else if (o2 is string && Numeric.IsNumeric (o1))
					o2 = (IComparable) Convert.ChangeType (o2, o1.GetType ());
				else if (o1 is string && o2 is Guid)
					o2 = o2.ToString ();
				else if (o2 is string && o1 is Guid)
					o1 = o1.ToString ();
			} catch (FormatException) {
				throw new EvaluateException (String.Format ("Cannot perform compare operation on {0} and {1}.", o1.GetType(), o2.GetType()));
			}

			if (o1 is string && o2 is string) {
				o1 = ((string) o1).TrimEnd (IgnoredTrailingChars);
				o2 = ((string) o2).TrimEnd (IgnoredTrailingChars);
				if (!caseSensitive) {
					o1 = ((string) o1).ToLower ();
					o2 = ((string) o2).ToLower ();
				}
			}

			if (o1 is DateTime && o2 is string && Thread.CurrentThread.CurrentCulture != CultureInfo.InvariantCulture) {
				// DateTime is always CultureInfo.InvariantCulture
				o2 = (IComparable) DateTime.Parse ((string)o2, CultureInfo.InvariantCulture);
			} else if (o2 is DateTime && o1 is string && 
			           Thread.CurrentThread.CurrentCulture != CultureInfo.InvariantCulture) {
				// DateTime is always CultureInfo.InvariantCulture
                o1 = (IComparable) DateTime.Parse ((string)o1, CultureInfo.InvariantCulture);
			}
			else if (o2 is DateTime && o1 is string && Thread.CurrentThread.CurrentCulture != CultureInfo.InvariantCulture) {
				// DateTime is always CultureInfo.InvariantCulture
				o1 = (IComparable) DateTime.Parse ((string)o1, CultureInfo.InvariantCulture);
			}

			if (o1.GetType () != o2.GetType ())
				o2 = (IComparable)Convert.ChangeType (o2, o1.GetType ());

			return o1.CompareTo (o2);
		}
开发者ID:nlhepler,项目名称:mono,代码行数:44,代码来源:Comparison.cs

示例6: ValidateRangeValidator

 internal static void ValidateRangeValidator(IComparable lowerBound, RangeBoundaryType lowerBoundaryType, IComparable upperBound, RangeBoundaryType upperBoundaryType)
 {
     if ((lowerBoundaryType != RangeBoundaryType.Ignore) && (lowerBound == null))
     {
         throw new ArgumentNullException("lowerBound");
     }
     if ((upperBoundaryType != RangeBoundaryType.Ignore) && (upperBound == null))
     {
         throw new ArgumentNullException("upperBound");
     }
     if ((lowerBoundaryType == RangeBoundaryType.Ignore) && (upperBoundaryType == RangeBoundaryType.Ignore))
     {
         throw new ArgumentException(Resources.ExceptionCannotIgnoreBothBoundariesInRange, "lowerBound");
     }
     if (((lowerBound != null) && (upperBound != null)) && (lowerBound.GetType() != upperBound.GetType()))
     {
         throw new ArgumentException(Resources.ExceptionTypeOfBoundsMustMatch, "upperBound");
     }
 }
开发者ID:davinx,项目名称:himedi,代码行数:19,代码来源:ValidatorArgumentsValidatorHelper.cs

示例7: TryCompareTo

        /// <summary>
        /// Compares the specified value with from value.
        /// </summary>
        /// <param name="sourceValue">The source value.</param>
        /// <param name="targetValue">The target value.</param>
        /// <param name="result">A value that indicates the relative order of the objects being compared.</param>
        /// <returns><c>true</c> if two values could be compared, otherwise <c>false</c>.</returns>
        public static bool TryCompareTo(IComparable sourceValue, IComparable targetValue, out int result)
        {
            if (sourceValue == null)
            {
                throw new ArgumentNullException("sourceValue");
            }

            if (targetValue == null)
            {
                throw new ArgumentNullException("targetValue");
            }

            if (!sourceValue.GetType().IsInstanceOfType(targetValue))
            {
                result = 0;
                return false;
            }

            result = sourceValue.CompareTo(targetValue);
            return true;
        }
开发者ID:QuickOrBeDead,项目名称:Labo.Validation,代码行数:28,代码来源:ComparableUtils.cs

示例8: EvaluateComparable

        /// <summary>
        /// Evaluates both operands that implement the IComparable interface.
        /// </summary>
        /// <param name="leftOperand">Left operand from the LeftOperand property.</param>
        /// <param name="operatorType">Operator from Operator property.</param>
        /// <param name="rightOperand">Right operand from the RightOperand property.</param>
        /// <returns>Returns true if the condition is met; otherwise, returns false.</returns>
        private static bool EvaluateComparable(IComparable leftOperand, ComparisonConditionType operatorType, IComparable rightOperand)
        {
            object obj2 = null;
            try
            {
                obj2 = Convert.ChangeType(rightOperand, leftOperand.GetType(), CultureInfo.CurrentCulture);
            }
            catch (FormatException)
            {
            }
            catch (InvalidCastException)
            {
            }
            if (obj2 == null)
            {
                return (operatorType == ComparisonConditionType.NotEqual);
            }
            int num = leftOperand.CompareTo((IComparable)obj2);
            switch (operatorType)
            {
                case ComparisonConditionType.Equal:
                    return (num == 0);

                case ComparisonConditionType.NotEqual:
                    return (num != 0);

                case ComparisonConditionType.LessThan:
                    return (num < 0);

                case ComparisonConditionType.LessThanOrEqual:
                    return (num <= 0);

                case ComparisonConditionType.GreaterThan:
                    return (num > 0);

                case ComparisonConditionType.GreaterThanOrEqual:
                    return (num >= 0);
            }
            return false;
        }
开发者ID:xperiandri,项目名称:Xaml.Interactions,代码行数:47,代码来源:ComparisonLogic.cs

示例9: IdentifyType

        /// <summary>
        /// Determines the appropriate facet type based upon the Type of a given instance.
        /// </summary>
        /// <remarks>
        /// This method will inspect the given object's type and attempt to match it to the expected Type(s) for each
        /// supported Pivot facet category type. For strings, it will automatically differentiate between the LongString
        /// and String types based upon the actual length of the given text.
        /// </remarks>
        /// <param name="value">the object to use in inferring the facet category type</param>
        /// <returns>the appropriate constant for the type most closely matching the given object</returns>
        /// <exception cref="ArgumentException">
        /// if no suitable type could be determined for the given object
        /// </exception>
        public static PivotFacetType IdentifyType(IComparable value)
        {
            if (value == null) throw new ArgumentNullException("Cannot determine the type of a null value");

            if (DateTime.IsValidValue(value)) return DateTime;
            if (Link.IsValidValue(value)) return Link;
            if (Number.IsValidValue(value)) return Number;

            if (String.IsValidValue(value))
            {
                String testString = (String)value;
                if (testString.Length > LongStringThreshold) return LongString;
                return String;
            }

            throw new ArgumentException("No facet type matches Type: " + value.GetType().Name);
        }
开发者ID:saviourofdp,项目名称:pauthor,代码行数:30,代码来源:PivotFacetType.cs

示例10: ContainsObjectIds

 /// <summary>
 /// Determines if this value has an object id (that is, is of type <see cref="LabeledInstance"/>),
 /// or has a subvalue that has an object id (for example, a set of instances).
 /// </summary>
 /// <returns>True if this value has an object id or contains a value with an object id.</returns>
 /// <remarks> This method is invoked by the modeling library when determining whether two states are isomorphic.
 /// </remarks>
 public static bool ContainsObjectIds(IComparable obj)
 {
     if (null == obj) return false;
     Type t = obj.GetType();
     if (GetLiteralTypes().ContainsKey(t) || t.IsEnum) return false;
     IAbstractValue av = obj as IAbstractValue;
     if (null == av) return false;  // maybe throw exception?
     else return av.ContainsObjectIds();
 }
开发者ID:juhan,项目名称:NModel,代码行数:16,代码来源:AbstractValue.cs

示例11: CheckPropertyType

 protected static void CheckPropertyType(String myVertexTypeName,
                                         IComparable myValue,
                                         IPropertyDefinition propertyDef)
 {
     //Assign safty should be suffice.
     if (!propertyDef.BaseType.IsAssignableFrom(myValue.GetType()))
         throw new PropertyHasWrongTypeException(myVertexTypeName,
                                                 propertyDef.Name,
                                                 propertyDef.BaseType.Name,
                                                 myValue.GetType().Name);
 }
开发者ID:anukat2015,项目名称:sones,代码行数:11,代码来源:AVertexHandler.cs

示例12: EvaluateComparable

        /// <summary>
        /// Evaluates both operands that implement the IComparable interface.
        /// </summary>
        private static bool EvaluateComparable(IComparable leftOperand, ComparisonConditionType operatorType, IComparable rightOperand)
        {
            object convertedOperand = null;
            try
            {
                convertedOperand = Convert.ChangeType(rightOperand, leftOperand.GetType(), CultureInfo.CurrentCulture);
            }
            catch (FormatException)
            {
                // FormatException: Convert.ChangeType("hello", typeof(double), ...);
            }
            catch (InvalidCastException)
            {
                // InvalidCastException: Convert.ChangeType(4.0d, typeof(Rectangle), ...);
            }

            if (convertedOperand == null)
            {
                return operatorType == ComparisonConditionType.NotEqual;
            }

            int comparison = leftOperand.CompareTo((IComparable)convertedOperand);
            switch (operatorType)
            {
                case ComparisonConditionType.Equal:
                    return comparison == 0;

                case ComparisonConditionType.NotEqual:
                    return comparison != 0;

                case ComparisonConditionType.LessThan:
                    return comparison < 0;

                case ComparisonConditionType.LessThanOrEqual:
                    return comparison <= 0;

                case ComparisonConditionType.GreaterThan:
                    return comparison > 0;

                case ComparisonConditionType.GreaterThanOrEqual:
                    return comparison >= 0;
            }

            return false;
        }
开发者ID:JackieyLi,项目名称:XamlBehaviors,代码行数:48,代码来源:DataTriggerBehavior.cs

示例13: Compare

        protected int Compare(IComparable lhs, IComparable rhs)
        {
            if (lhs == null || rhs == null)
                return lhs?.CompareTo(rhs) ?? (-1*rhs?.CompareTo(lhs) ?? 0);

            if(TypeConverter.IsNumeric(lhs) && TypeConverter.IsNumeric(rhs))
            {
                lhs = (decimal) TypeConverter.To(lhs, typeof(decimal));
                rhs = (decimal) TypeConverter.To(rhs, typeof(decimal));
            }
            if (!Equals(lhs.GetType(), rhs.GetType()))
            {
                throw ComparisionException.UnComparable(lhs.GetType(), rhs.GetType()).Decorate(Token);
            }
            return lhs.CompareTo(rhs);
        }
开发者ID:rslijp,项目名称:sharptiles,代码行数:16,代码来源:ComparisonExpression.cs

示例14: IsCompatible

 public bool IsCompatible(IComparable value)
 {
     return value.GetType() == typeof (int);
 }
开发者ID:SzymonPobiega,项目名称:ReferenceDataManager,代码行数:4,代码来源:TimelineTests.cs

示例15: AssertValidValue

 public override void AssertValidValue(IComparable value)
 {
     if (this.IsValidValue(value) == false)
     {
         throw new ArgumentException("Invalid type for String facet: " + value.GetType().Name);
     }
 }
开发者ID:saviourofdp,项目名称:pauthor,代码行数:7,代码来源:PivotFacetType.cs


注:本文中的IComparable.GetType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。