Search code examples
c#regexvalidationpropertiestypebuilder

Validate property name (checking for illegal characters)


For marshalling purpose and automatic type building with native API, I need to validate field names for some structure during runtime. The naming rules on native side are the same as in C# (no whitespace, no special characters like &, é, *, .).

Is there a standard regex pattern for that ?

NB: As workaround I'm thinking of building static method around DeclareProperty in TypeBuilder just for name validation purpose.


Solution

  • Should be enough for what I want so far:

    ^[a-zA-Z]+[a-zA-Z0-9\_]*$