.net 3.5中的TryParse [英] TryParse in .net 3.5

查看:109
本文介绍了.net 3.5中的TryParse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



请参阅下面的ASP.NET 4.5代码

  if (!Enum.TryParse(request.QueryString [  q], out  cp.QuietZones))
cp.QuietZones = QuietZoneModules.Two;





我想要相应的3.5代码。 Enum.TryParse()不在3.5中。请帮帮我。



提前感谢,

Manu V Nath

解决方案

< blockquote> TryParse具有以下签名:



 ryParse< tenum>( string   value  bool  ignoreCase, out  TEnum结果)
其中 TEnum: struct new ()
< / tenum >





它有一个泛型类型参数

 TEnum 

必须是

  struct  

并且它用于确定

 枚举 

它正在解析。如果不明确地提供它(就像你做的那样),它将采用resultInputType变量的类型,即Enum(而不是枚举本身的类型)。请注意,

 Enum 

是一个类(甚至很难从ValueType继承),因此它不满足

 TEnum 

  struct  





您可以通过为方法提供泛型参数而不是Type参数来解决此问题。泛型类型参数

 TEnum 

必须具有与TryParse函数上相同参数相同的约束(即struct)。否则你可以在方法中放一个类,而

 TryParse 

不接受。



所以试试这个:



  private   static  TEnum getEnumStringEnumType< tenum>()
其中 TEnum: struct
{
string userInputString = string .Empty;
TEnum resultInputType = 默认(TEnum);
bool enumParseResult = false ;

while (!enumParseResult)
{
userInputString = System.Console.ReadLine();
enumParseResult = Enum.TryParse(userInputString, true out resultInputType);
}
return resultInputType;
}
< / tenum >





要调用此方法,请使用:



 getEnumStringEnumType< myenum>(); < /   myenum  >  


您好,



如StackOverFlow所示:

  public   static   bool  EnumTryParse< T>( string  strType, out  T结果)
{
string strTypeFixed = strType.Replace(' '' _' );
if (Enum.IsDefined( typeof (T),strTypeFixed))
{
result =(T)Enum.Parse( typeof (T),strTypeFixed, true );
return true ;
}
其他
{
foreach (< span class =code-keyword> string value Enum.GetNames( typeof (T)))
{
if value .Equals(strTypeFixed,StringComparison.OrdinalIgnoreCase))
{
result =(T)Enum.Parse( typeof (T), value );
return true ;
}
}
result = 默认(T);
return false ;
}
}


请尝试此链接

http://damieng.com/blog/2010/10/17/ enums-better-syntax-improved-performance-and-tryparse-in-net-3-5 [ ^ ]



希望这有助于


Hi all,

Please see below ASP.NET 4.5 code

 if ( !Enum.TryParse( request.QueryString["q"], out cp.QuietZones ) )
cp.QuietZones = QuietZoneModules.Two;



I want corresponding 3.5 code. Enum.TryParse() is not in 3.5. Please help me.

thanks in advance,
Manu V Nath

解决方案

TryParse has the following signature:

ryParse<tenum>(string value, bool ignoreCase, out TEnum result)
    where TEnum : struct, new()
</tenum>



It has a generic type parameter

TEnum 

that must be a

struct 

and that it uses to determine the type of

enum 

that it is parsing. When you don't provide it explicitly (as you did), it will take the type of the resultInputType variable, which is Enum (and not the type of the enumeration itself). Note that

Enum 

is a class (even tough it inherits from ValueType) and therefore it does not satisfy the requirement that

TEnum 

is a

struct

.

You can solve this by giving the method a generic argument instead of a Type parameter. The generic type parameter

TEnum 

must have the same constraints (i.e. struct) as the same parameter on the TryParse function. Otherwise you could put a class in the method while

TryParse 

does not accept that.

So try this:

private static TEnum getEnumStringEnumType<tenum>()
    where TEnum : struct
{
    string userInputString = string.Empty;
    TEnum resultInputType = default(TEnum);
    bool enumParseResult = false;

    while (!enumParseResult)
    {                
        userInputString = System.Console.ReadLine();
        enumParseResult = Enum.TryParse(userInputString, true, out resultInputType);
    }
    return resultInputType;
}
</tenum>



To call the method, use:

getEnumStringEnumType<myenum>();</myenum>


Hello,

As seen on StackOverFlow :

public static bool EnumTryParse<T>(string strType,out T result)
{
    string strTypeFixed = strType.Replace(' ', '_');
    if (Enum.IsDefined(typeof(T), strTypeFixed))
    {
        result = (T)Enum.Parse(typeof(T), strTypeFixed, true);
        return true;
    }
    else
    {
        foreach (string value in Enum.GetNames(typeof(T)))
        {
            if (value.Equals(strTypeFixed, StringComparison.OrdinalIgnoreCase))
            {
                result = (T)Enum.Parse(typeof(T), value);
                return true;
            }
        }
        result = default(T);
        return false;
    }
}


Please try this link
http://damieng.com/blog/2010/10/17/enums-better-syntax-improved-performance-and-tryparse-in-net-3-5[^]

Hope this helps


这篇关于.net 3.5中的TryParse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆