C# 如何为任何数据类型C模拟一种maxlength#

C# 如何为任何数据类型C模拟一种maxlength#,c#,C#,我试图用返回bool的方法验证文本的maxlength public bool ExceedsMaxLength(object value) { if(this.MyPropertyType == typeof(String)) { return ((string)value).Length > this.MaximumAllowed; } //Numeric????? } 我试着用这种方法来做这件事 if(this.MyPr

我试图用返回bool的方法验证文本的maxlength

public bool ExceedsMaxLength(object value)
{
     if(this.MyPropertyType == typeof(String))
     {
           return ((string)value).Length > this.MaximumAllowed;
     }
     //Numeric?????
}
我试着用这种方法来做这件事

if(this.MyPropertyType == typeof(Int16))
{
     return ((short)value > Int16.MaxValue);
}
我的方法对吗?这是确定的,我应该这样做的任何数字数据类型?或者有另一种简单的方法可以用.NET中的特殊方法来实现这一点


谢谢

由于您提到您只对基于字符串表示的最大长度上限感兴趣,下面的代码将满足您的需要:

public bool IsOverMaximumLength(object value)
{
    return (value.ToString().Length > this.MaximumAllowed);
}
如果要检查多种数据类型的长度,则更适合使用更多方法或重载:

public bool IsOverMaximumLengthForInt32(long value)
{
    return value > Int32.MaxValue;
}

public bool IsOverMaximumLengthForInt16(int value)
{
    return value > Int16.MaxValue;
}
这是一种反思方法,也可能适合您的需要:

public static bool ExceedsMaximumValue(object source, object destination)
{
    Type sourceType = source.GetType();
    FieldInfo sourceMaxValue = sourceType.GetField("MaxValue");

    if (Object.ReferenceEquals(sourceMaxValue, null))
    {
        throw new ArgumentException("The source object type does not have a MaxValue field associated with it.");
    }

    Type destinationType = destination.GetType();
    FieldInfo destinationMaxValue = destinationType.GetField("MaxValue");

    if (Object.ReferenceEquals(destinationMaxValue, null))
    {
        throw new ArgumentException("The destination object type does not have a MaxValue field associated with it.");
    }

    object convertedSource;
    if (destinationType.IsAssignableFrom(sourceType))
    {
        convertedSource = source;
    }
    else
    {
        TypeConverter converter = TypeDescriptor.GetConverter(sourceType);
        if (converter.CanConvertTo(destinationType))
        {
            try
            {
                convertedSource = converter.ConvertTo(source, destinationType);
            }
            catch (OverflowException)
            {
                return true;
            }
        }
        else
        {
            throw new ArgumentException("The source object type cannot be converted to the destination object type.");
        }
    }

    Type convertedSourceType = convertedSource.GetType();

    Type[] comparisonMethodParameterTypes = new Type[1]
    {
        destinationType
    };

    MethodInfo comparisonMethod = convertedSourceType.GetMethod("CompareTo", comparisonMethodParameterTypes);
    if (Object.ReferenceEquals(comparisonMethod, null))
    {
        throw new ArgumentException("The source object type does not have a CompareTo method.");
    }

    object[] comparisonMethodParameters = new object[1]
    {
        destination
    };

    int comparisonResult = (int)comparisonMethod.Invoke(convertedSource, comparisonMethodParameters);

    if (comparisonResult > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

“MaxLength”应该从数字类型返回什么样的信息?起初,它计划控制字符串值,但如果您可以看到参数是一个对象,那么我想模拟它可以传递任何数据类型,如果大小大于允许的大小,则返回true或false。在string中,我有一个仅允许用于string的属性maximumalowed,它必须与数据库表的列大小相匹配。在其他类型中,它不应大于其类型您的代码读取
return((short)value>Int16.MaxValue)
将始终返回false,因为当您显式转换为
short
时,会发生有损转换。谢谢您的回答。正如您所说,我需要对我的字符串类型进行修复,但我希望控制其他数据类型。这是我的问题。Regard我建议使用重载或简单地使用更多方法来补充您的
ExceedsMaxLength
方法。我将编辑我的帖子以向您展示。
IsOverMaximumLengthForInt16
不妨阅读
return true
。没有任何
int
值大于
Int32。MaxValue
@Servy感谢您指出,这实际上是一个错误。已更正,并添加了另一个样品。