Created
November 30, 2014 22:22
-
-
Save greggman/41019089e33b516f9fc5 to your computer and use it in GitHub Desktop.
Print out stuff about fields of an object in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vector2 v = new Vector2(2f,3f); | |
System.Text.StringBuilder s = new System.Text.StringBuilder(); | |
System.Reflection.FieldInfo[] fields = v.GetType().GetFields(); | |
s.Append("numField: " + fields.Length + "\n"); | |
// foreach (System.Reflection.FieldInfo info in fields) { | |
for (int ii = 0; ii < fields.Length; ++ii) { | |
System.Reflection.FieldInfo info = fields[ii]; | |
System.Type t = info.GetType(); | |
s.Append("[" + ii + "]"); | |
s.Append(info.Name); | |
if (info.IsPublic) { | |
s.Append(" IsPublic(info) "); | |
} | |
if (info.IsPrivate) { | |
s.Append(" IsPrivate(info) "); | |
} | |
if (info.IsStatic) { | |
s.Append(" IsStatic(info) "); | |
} | |
System.Reflection.TypeAttributes attr = t.Attributes; | |
if (t.IsAbstract) { s.Append(" IsAbstract "); | |
} | |
if (t.IsAnsiClass) { s.Append(" IsAnsiClass "); | |
} | |
if (t.IsArray) { s.Append(" IsArray "); | |
} | |
if (t.IsAutoClass) { s.Append(" IsAutoClass "); | |
} | |
if (t.IsAutoLayout) { s.Append(" IsAutoLayout "); | |
} | |
if (t.IsByRef) { s.Append(" IsByRef "); | |
} | |
if (t.IsClass) { s.Append(" IsClass "); | |
} | |
if (t.IsCOMObject) { s.Append(" IsCOMObject "); | |
} | |
if (t.IsContextful) { s.Append(" IsContextful "); | |
} | |
if (t.IsEnum) { s.Append(" IsEnum "); | |
} | |
if (t.IsExplicitLayout) { s.Append(" IsExplicitLayout "); | |
} | |
if (t.IsGenericParameter) { s.Append(" IsGenericParameter "); | |
} | |
if (t.IsGenericType) { s.Append(" IsGenericType "); | |
} | |
if (t.IsGenericTypeDefinition) { s.Append(" IsGenericTypeDefinition "); | |
} | |
if (t.IsImport) { s.Append(" IsImport "); | |
} | |
if (t.IsInterface) { s.Append(" IsInterface "); | |
} | |
if (t.IsLayoutSequential) { s.Append(" IsLayoutSequential "); | |
} | |
if (t.IsMarshalByRef) { s.Append(" IsMarshalByRef "); | |
} | |
if (t.IsNested) { s.Append(" IsNested "); | |
} | |
if (t.IsNestedAssembly) { s.Append(" IsNestedAssembly "); | |
} | |
if (t.IsNestedFamANDAssem) { s.Append(" IsNestedFamANDAssem "); | |
} | |
if (t.IsNestedFamily) { s.Append(" IsNestedFamily "); | |
} | |
if (t.IsNestedFamORAssem) { s.Append(" IsNestedFamORAssem "); | |
} | |
if (t.IsNestedPrivate) { s.Append(" IsNestedPrivate "); | |
} | |
if (t.IsNestedPublic) { s.Append(" IsNestedPublic "); | |
} | |
if (t.IsNotPublic) { s.Append(" IsNotPublic "); | |
} | |
if (t.IsPointer) { s.Append(" IsPointer "); | |
} | |
if (t.IsPrimitive) { s.Append(" IsPrimitive "); | |
} | |
if (t.IsPublic) { s.Append(" IsPublic "); | |
} | |
if (t.IsSealed) { s.Append(" IsSealed "); | |
} | |
if (t.IsSerializable) { s.Append(" IsSerializable "); | |
} | |
if (t.IsSpecialName) { s.Append(" IsSpecialName "); | |
} | |
if (t.IsUnicodeClass) { s.Append(" IsUnicodeClass "); | |
} | |
if (t.IsValueType) { s.Append(" IsValueType "); | |
} | |
if (t.IsVisible) { s.Append(" IsVisible "); | |
} | |
// To test for visibility attributes, you must use the visibility mask. | |
System.Reflection.TypeAttributes visibility = attr & System.Reflection.TypeAttributes.VisibilityMask; | |
switch (visibility) | |
{ | |
case System.Reflection.TypeAttributes.NotPublic: | |
s.Append(" ...is not public"); | |
break; | |
case System.Reflection.TypeAttributes.Public: | |
s.Append(" ...is public"); | |
break; | |
case System.Reflection.TypeAttributes.NestedPublic: | |
s.Append(" ...is nested and public"); | |
break; | |
case System.Reflection.TypeAttributes.NestedPrivate: | |
s.Append(" ...is nested and private"); | |
break; | |
case System.Reflection.TypeAttributes.NestedFamANDAssem: | |
s.Append(" ...is nested, and inheritable only within the assembly" + | |
"\n (cannot be declared in C#)"); | |
break; | |
case System.Reflection.TypeAttributes.NestedAssembly: | |
s.Append(" ...is nested and internal"); | |
break; | |
case System.Reflection.TypeAttributes.NestedFamily: | |
s.Append(" ...is nested and protected"); | |
break; | |
case System.Reflection.TypeAttributes.NestedFamORAssem: | |
s.Append(" ...is nested and protected internal"); | |
break; | |
default: | |
s.Append("what!(1)"); | |
break; | |
} | |
//' Use the layout mask to test for layout attributes. | |
System.Reflection.TypeAttributes layout = attr & System.Reflection.TypeAttributes.LayoutMask; | |
switch (layout) | |
{ | |
case System.Reflection.TypeAttributes.AutoLayout: | |
s.Append(" ...is AutoLayout"); | |
break; | |
case System.Reflection.TypeAttributes.SequentialLayout: | |
s.Append(" ...is SequentialLayout"); | |
break; | |
case System.Reflection.TypeAttributes.ExplicitLayout: | |
s.Append(" ...is ExplicitLayout"); | |
break; | |
default: | |
s.Append("what!(2)"); | |
break; | |
} | |
//' Use the class semantics mask to test for class semantics attributes. | |
System.Reflection.TypeAttributes classSemantics = attr & System.Reflection.TypeAttributes.ClassSemanticsMask; | |
switch (classSemantics) | |
{ | |
case System.Reflection.TypeAttributes.Class: | |
if (t.IsValueType) | |
{ | |
s.Append(" ...is a value type"); | |
} | |
else | |
{ | |
s.Append(" ...is a class"); | |
} | |
break; | |
case System.Reflection.TypeAttributes.Interface: | |
s.Append(" ...is an interface"); | |
break; | |
default: | |
s.Append("what!(3)"); | |
break; | |
} | |
if (0!=(attr & System.Reflection.TypeAttributes.Abstract)) | |
{ | |
s.Append(" ...is abstract"); | |
} | |
if (0!=(attr & System.Reflection.TypeAttributes.Sealed)) | |
{ | |
s.Append(" ...is sealed"); | |
} | |
foreach (System.Attribute cattr in t.GetCustomAttributes(true)) { | |
s.Append(" a:" + cattr.ToString() + " "); | |
} | |
s.Append("\n"); | |
} | |
Debug.Log(s.ToString()); | |
Debug.Log(DeJson.Serializer.Serialize(v)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment