-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathTypeReferenceWriter.cs
56 lines (47 loc) · 1.56 KB
/
TypeReferenceWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.Text;
namespace Xamarin.SourceWriter
{
public class TypeReferenceWriter
{
public string Namespace { get; set; }
public string Name { get; set; }
public bool Nullable { get; set; }
// These purposely create new instances, as they are not immutable.
// For example you may intend to make an instance null, but if there
// was only one, you would make them all null.
public static TypeReferenceWriter Bool => new TypeReferenceWriter ("bool");
public static TypeReferenceWriter Delegate => new TypeReferenceWriter ("Delegate");
public static TypeReferenceWriter IntPtr => new TypeReferenceWriter ("IntPtr");
public static TypeReferenceWriter Float => new TypeReferenceWriter ("float");
public static TypeReferenceWriter Object => new TypeReferenceWriter ("object");
public static TypeReferenceWriter Void => new TypeReferenceWriter ("void");
public TypeReferenceWriter (string name)
{
var index = name.LastIndexOf ('.');
if (index >= 0) {
Namespace = name.Substring (0, index);
Name = name.Substring (index + 1);
} else {
Name = name;
}
}
public TypeReferenceWriter (string ns, string name)
{
Namespace = ns;
Name = name;
}
public virtual void WriteTypeReference (CodeWriter writer)
{
writer.Write ($"{ToString ()} ");
}
string NullableOperator => Nullable ? "?" : string.Empty;
public override string ToString ()
{
if (Namespace.HasValue ())
return $"{Namespace}.{Name}{NullableOperator}";
return $"{Name}{NullableOperator}";
}
}
}