|
using System;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;
namespace Params
{
public class Helper
{
public static object GetValue(object o, string key, object defaultValue)
{
if(o is IDictionary)
{
IDictionary dict = o as IDictionary;
if(dict.Contains(key))
return dict[key];
else
return defaultValue;
}
if(o is NameValueCollection)
{
NameValueCollection nvc = o as NameValueCollection;
if(nvc[key] != null)
return nvc[key];
else
return defaultValue;
}
if(o is XmlAttributeCollection)
{
XmlAttributeCollection xac = o as XmlAttributeCollection;
if(xac[key] != null)
return xac[key].Value;
else
return defaultValue;
}
throw new InvalidOperationException();
}
public static bool GetBool(object o, string key, bool defaultValue)
{
object ret = GetValue(o, key, null);
if(ret == null)
return defaultValue;
else
return Convert.ToBoolean(ret);
}
public static int GetInt(object o, string key, int defaultValue)
{
object ret = GetValue(o, key, null);
if(ret == null)
return defaultValue;
else
return Convert.ToInt32(ret);
}
public static float GetFloat(object o, string key, float defaultValue)
{
object ret = GetValue(o, key, null);
if(ret == null)
return defaultValue;
else
return Convert.ToSingle(ret);
}
public static Enum GetEnum(object o, string key, Enum defaultValue, Type enumType)
{
object ret = GetValue(o, key, defaultValue);
if(ret is string)
return (Enum)Enum.Parse(enumType, (string)ret, true);
else
return (Enum)ret;
}
public static string GetString(object o, string key, string defaultValue)
{
return GetValue(o, key, defaultValue) as string;
}
public static int BeginsWith(string str, int offset, params string[] patterns)
{
for(int i = 0; i < patterns.Length; i++)
{
string pattern = patterns[i];
if( pattern != null &&
pattern != string.Empty &&
str.Length >= (pattern.Length + offset) &&
str.Substring(offset, pattern.Length).Equals(pattern))
{
return i;
}
}
return -1;
}
public static int GetNextParamIndex(string cmdLine, int offset, string shortControlString, string longControlString)
{
int valueEndIndexShort = cmdLine.IndexOf(shortControlString, offset);
int valueEndIndexLong = cmdLine.IndexOf(longControlString, offset);
if(valueEndIndexShort == -1 && valueEndIndexLong == -1)
return cmdLine.Length-1;
else if(valueEndIndexLong == -1)
return valueEndIndexShort;
else if(valueEndIndexShort == -1)
return valueEndIndexLong;
else
{
return Math.Min(valueEndIndexLong, valueEndIndexShort);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Just you average programmer geek from Romania. Student at Faculty of Computer Science, Al. I. Cuza University Iasi, Romania.