What's an ansi class in C#?
Asked Answered
S

2

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);
Szombathely answered 11/3, 2015 at 14:24 Comment(8)
Can you show some relevant code? MSDN links perhaps? It seems to have to do with string marshaling during interop though.Merbromin
Whenever you have questions like this, the MSDN documentation is a great resource. TypeAttributes on MSDN: msdn.microsoft.com/en-us/library/…Determined
Related documentation: msdn.microsoft.com/en-us/library/… The remarks indicates that the ansi part is regarding string formats (encodings).Galacto
Okay, so great minds think alike... ;) Perhaps a duplicate of https://mcmap.net/q/25700/-what-is-ansi-format ?Galacto
See also Default Marshaling for Strings.Merbromin
@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 forCopyright
The msdn doc has the same information as the one in the TypeAttributes enum. It's useless.Szombathely
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.Nabila
B
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
Blubber answered 11/3, 2015 at 14:48 Comment(0)
E
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

Entertainer answered 11/3, 2015 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.