1

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
  • I would +1 your edit it it were an answer. It seems like the right tool for the job in a UNIX program kind of way. Commented Sep 17, 2012 at 14:00

2 Answers 2

1

Script# is one thing to investigate.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

5 Comments

Why not use Enum.GetValues(myEnumType) instead??
You mean Enum.GetNames(). Yes, that would be simpler in this case. The OP question was trying to use reflection though, so I gave the method of accessing enum values via reflection.
^ And BTW, you'd still have to use reflection to get the enum type in this scenario since you're loading an external assembly to generate code. It is just the .GetMembers().Where(m => m.MemberType == MemberTypes.Field) that would get replaced.
I have no argument about the first 2 lines that loads the type (That is why I used the same variable name that you wrote). You should use Enum.GetNames or Enum.GetValues instead of that foreach. And if you need both names and values (which is in most cases), Enum.GetNames will result in using Enum.Parse for each name, but by using Enum.GetValues, You have the names (.ToString()) and values together
It depends on the code being generated. You might need the values, but I would advise against for things like a URL parameters because an integral value is less readable. You get a lot more data with the MemberInfo. When using reflection to generate code you often need to look at custom attributes as well, for example using the System.ComponentModel.DescriptionAttribute you can pull a label to be shown to the user--this is something I almost always end up doing when generating code from enums for web clients, and you can't do using any of the Enum static methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.