views:

344

answers:

2

I have a flags enumeration in a .NET assembly that is getting called from an ASP.NET page. I want to have a Visual Studio build step generate a .js file that has the JavaScript equivalent in it. Are there any tools for doing this?


edit: This seems to work.

public class JavaScriptReflection
{
    public static string Go(Type type)
    {
        if (!type.IsEnum) return;

        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("var {0} = {{ ", type.Name);

        foreach (FieldInfo fInfo in 
            type.GetFields(BindingFlags.Public | BindingFlags.Static))

            sb.AppendFormat("{0}:{1},\r\n", 
                fInfo.Name,
                fInfo.GetRawConstantValue().ToString());

        sb.Append("};");
        return sb.toString();
    }
}
+1  A: 

I've had sucess recently using reflection on output assembly files to generate code.

Try using something like this in a console app you can call from your post-build process:

Assembly assembly = Assembly.LoadFile("FileName");
Type myEnumType = assembly.GetType("EnumName");
foreach(MemberInfo mi in myEnumType.GetMembers().Where(m => m.MemberType == MemberTypes.Field))
        Console.WriteLine(mi.Name);
Paul
+1  A: 

Script# is one thing to investigate.

RichardOD