| 1 |
using System; |
| 2 |
|
| 3 |
using Mono.Cecil; |
| 4 |
|
| 5 |
using Mono.Documentation.Util; |
| 6 |
|
| 7 |
namespace Mono.Documentation.Updater |
| 8 |
{ |
| 9 |
/// <summary>Formats attribute values. Should return true if it is able to format the value.</summary> |
| 10 |
class AttributeValueFormatter |
| 11 |
{ |
| 12 |
public virtual bool TryFormatValue (object v, ResolvedTypeInfo type, out string returnvalue) |
| 13 |
{ |
| 14 |
TypeReference valueType = type.Reference; |
| 15 |
if (v == null) |
| 16 |
{ |
| 17 |
returnvalue = "null"; |
| 18 |
return true; |
| 19 |
} |
| 20 |
if (valueType.FullName == "System.Type") |
| 21 |
{ |
| 22 |
var vTypeRef = v as TypeReference; |
| 23 |
if (vTypeRef != null) |
| 24 |
returnvalue = "typeof(" + NativeTypeManager.GetTranslatedName (vTypeRef) + ")"; // TODO: drop NS handling |
| 25 |
else |
| 26 |
returnvalue = "typeof(" + v.ToString () + ")"; |
| 27 |
|
| 28 |
return true; |
| 29 |
} |
| 30 |
if (valueType.FullName == "System.String") |
| 31 |
{ |
| 32 |
returnvalue = "\"" + MDocUpdater.FilterSpecialChars (v.ToString ()) + "\""; |
| 33 |
return true; |
| 34 |
} |
| 35 |
if (valueType.FullName == "System.Char") |
| 36 |
{ |
| 37 |
returnvalue = "'" + MDocUpdater.FilterSpecialChars (v.ToString ()) + "'"; |
| 38 |
return true; |
| 39 |
} |
| 40 |
if (v is Boolean) |
| 41 |
{ |
| 42 |
returnvalue = (bool)v ? "true" : "false"; |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
TypeDefinition valueDef = type.Definition; |
| 47 |
if (valueDef == null || !valueDef.IsEnum) |
| 48 |
{ |
| 49 |
returnvalue = v.ToString (); |
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
string typename = MDocUpdater.GetDocTypeFullName (valueType); |
| 54 |
var values = MDocUpdater.GetEnumerationValues (valueDef); |
| 55 |
long c = MDocUpdater.ToInt64 (v); |
| 56 |
if (values.ContainsKey (c)) |
| 57 |
{ |
| 58 |
returnvalue = typename + "." + values[c]; |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
returnvalue = null; |
| 63 |
return false; |
| 64 |
} |
| 65 |
} |
| 66 |
} |