8

I started using reflection in my project.

When I create a type and I want to specify the TypeAttributes, I have two options: AnsiClass and Class. They are both set to 0 in the enum TypeAttributes.

public enum TypeAttributes
{
    //     LPTSTR is interpreted as ANSI.
    AnsiClass = 0,
    //
    // Summary:
    //     Specifies that the type is a class.
    Class = 0,
...

1- What's an ANSI Class (I noticed that, it's related to the LPTSTR, but I didn't understood well enough)?

2- What's the difference between an AnsiClass and a Class when I use Reflection.

EDIT

Here's an example of specifying the TypeAttributes in Reflection:

TypeBuilder typeBuilder = mbuilder.DefineType("DataRow", TypeAttributes.Public | TypeAttributes.AnsiClass | TypeAttributes.Class);
billybob
  • 2,597
  • 6
  • 26
  • 50
  • 1
    Can you show some relevant code? MSDN links perhaps? It [seems](http://referencesource.microsoft.com/#mscorlib/system/reflection/typeattributes.cs,ada9a877a9d95037,references) to have to do with string marshaling during interop though. – CodeCaster Mar 11 '15 at 14:27
  • Whenever you have questions like this, the MSDN documentation is a great resource. `TypeAttributes` on MSDN: https://msdn.microsoft.com/en-us/library/system.reflection.typeattributes%28v=vs.110%29.aspx – cbr Mar 11 '15 at 14:30
  • Related documentation: https://msdn.microsoft.com/en-us/library/system.reflection.typeattributes(v=vs.110).aspx The remarks indicates that the ansi part is regarding string formats (encodings). – sisve Mar 11 '15 at 14:30
  • Okay, so great minds think alike... ;) Perhaps a duplicate of http://stackoverflow.com/questions/701882/what-is-ansi-format ? – sisve Mar 11 '15 at 14:31
  • See also [Default Marshaling for Strings](https://msdn.microsoft.com/en-us/library/s9ts558h%28v=vs.110%29.aspx). – CodeCaster Mar 11 '15 at 14:35
  • 2
    @SimonSvensson I was reading the MSDN documentation before (and now that other SO question). The MSDN documentation tells me what the Type Attribute _is_, but doesn't really tell me what it is _for_ – Peter M Mar 11 '15 at 14:36
  • The msdn doc has the same information as the one in the TypeAttributes enum. It's useless. – billybob Mar 11 '15 at 14:38
  • 1
    You know it better as StructLayoutAttribute.CharSet. Every .NET type has it, even if you don't use the attribute explicitly. Ecma-335 is a good source for this kind of info. – Hans Passant Mar 11 '15 at 14:41

2 Answers2

3

I have two options: AnsiClass and Class

No, those are orthogonal. AnsiClass tells the marshaller that all string properties are to be marshalled as ANSI strings. The other options in that category are:

UnicodeClass
AutoClass
CustomFormatClass

Class indicates that they type is, well, a class. The only other option in that group is Interface. Interestingly, when you look at the attributes of framework types, classes, structs, and interfaces all have the Class attribute, so I'm not sure what it's used for.

 typeof(object).Attributes.Dump();   
 typeof(int).Attributes.Dump();   
 typeof(ICloneable).Attributes.Dump();  

output:

AutoLayout, AnsiClass, Class, Public, Serializable, BeforeFieldInit
AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, Serializable, BeforeFieldInit
AutoLayout, AnsiClass, Class, Public, ClassSemanticsMask, Abstract
D Stanley
  • 139,271
  • 11
  • 154
  • 219
0

well if you look at MSDN is says

https://msdn.microsoft.com/en-us/library/system.type.isansiclass%28v=vs.110%29.aspx

Property Value

Type: System.Boolean

true if the string format attribute AnsiClass is selected for the Type; otherwise, false.

Implements _Type.IsAnsiClass

which is rather less than useful but if you look at the the StringFormatMask of the TypeAttributes Enumeration, you will see it has the possible values of {AnsiClass,UnicodeClass,AutoClass,CustomFormatClass}

so basically it informs the compiler how to interpret the strings within the class as ANSI or Unicode, which as far as i'm aware has no impact on c# and vb but can lead to all sorts of issues if your using c++ classes

see http://www.codeproject.com/Articles/2995/The-Complete-Guide-to-C-Strings-Part-I-Win-Chara

Community
  • 1
  • 1
MikeT
  • 4,398
  • 2
  • 23
  • 34