Google translate API lib for DNX
This commit is contained in:
32
GoogleCode/GoogleTranslate/Misc/ExtensionMethods.cs
Normal file
32
GoogleCode/GoogleTranslate/Misc/ExtensionMethods.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace GoogleTranslateNET.Misc
|
||||
{
|
||||
public static class ExtensionMethods
|
||||
{
|
||||
/// <summary>
|
||||
/// Will get the string value for a given enums value, this will
|
||||
/// only work if you assign the StringValue attribute to
|
||||
/// the items in your enum.
|
||||
/// Source: http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetStringValue(this Enum value)
|
||||
{
|
||||
// Get the type
|
||||
Type type = value.GetType();
|
||||
|
||||
// Get fieldinfo for this type
|
||||
FieldInfo fieldInfo = type.GetField(value.ToString());
|
||||
|
||||
// Get the stringvalue attributes
|
||||
StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
|
||||
typeof(StringValueAttribute), false) as StringValueAttribute[];
|
||||
|
||||
// Return the first if there was a match.
|
||||
return attribs.Length > 0 ? attribs[0].StringValue : null;
|
||||
}
|
||||
}
|
||||
}
|
18
GoogleCode/GoogleTranslate/Misc/StringValueAttribute.cs
Normal file
18
GoogleCode/GoogleTranslate/Misc/StringValueAttribute.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
|
||||
namespace GoogleTranslateNET.Misc
|
||||
{
|
||||
/// <summary>
|
||||
/// This attribute is used to represent a string value
|
||||
/// for a value in an enum.
|
||||
/// </summary>
|
||||
public class StringValueAttribute : Attribute
|
||||
{
|
||||
public string StringValue { get; private set; }
|
||||
|
||||
public StringValueAttribute(string value)
|
||||
{
|
||||
StringValue = value;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user