19 using System.Collections.Generic;
21 using System.Reflection;
22 using System.Reflection.Emit;
35 private Type GenericMapType = typeof(Dictionary<,>);
40 private Type GenericListType = typeof(List<>);
45 private Type GenericNullableType = typeof(Nullable<>);
47 private readonly Assembly execAssembly;
48 private readonly Assembly entryAssembly;
49 private readonly
bool diffAssembly;
53 Dictionary<NameCtorKey, CtorDelegate> ctors;
57 execAssembly = System.Reflection.Assembly.GetExecutingAssembly();
58 entryAssembly = System.Reflection.Assembly.GetEntryAssembly();
59 if (entryAssembly !=
null && execAssembly != entryAssembly)
62 GenericMapType = typeof(Dictionary<,>);
63 GenericListType = typeof(List<>);
65 ctors =
new Dictionary<NameCtorKey, CtorDelegate>();
70 public string name {
get;
private set; }
82 public override bool Equals(
object obj)
84 if (ReferenceEquals(
null, obj))
103 return !left.
Equals(right);
113 private Type FindType(
string name,
bool throwError)
119 name = name.Replace(
"Nullable",
"Nullable`1");
120 name = name.Replace(
"IList<",
"System.Collections.Generic.IList`1[");
121 name = name.Replace(
"IDictionary<string,",
"System.Collections.Generic.IDictionary`2[System.String,");
122 name = name.Replace(
"IDictionary<",
"System.Collections.Generic.IDictionary`2[");
123 name = name.Replace(
"<",
"[");
124 name = name.Replace(
">",
"]");
129 type = entryAssembly.GetType(name);
131 type = Type.GetType(name);
134 type = Type.GetType(name);
140 Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
141 foreach (Assembly assembly
in assemblies)
144 if (assembly.FullName.StartsWith(
"MonoDevelop.NUnit"))
147 types = assembly.GetTypes();
150 foreach (Type t
in types)
152 if (name == t.Name || name == t.FullName) type = t;
160 if (
null == type && throwError)
162 throw new AvroException(
"Unable to find type " + name +
" in all loaded assemblies");
186 return typeof(
float);
188 return typeof(
double);
190 return typeof(
byte[]);
192 return typeof(
string);
196 if (
null != unSchema && unSchema.
Count==2)
202 Type itemType =
null;
212 if (
null != itemType )
214 if (itemType.IsValueType && !itemType.IsEnum)
218 return GenericNullableType.MakeGenericType(
new [] {itemType});
220 catch (Exception) { }
227 return typeof(
object);
233 return GenericListType.MakeGenericType(
new [] {itemSchema}); }
238 return GenericMapType.MakeGenericType(
new [] { typeof(
string), itemSchema }); }
246 return FindType(named.Fullname,
true);
252 return FindType(schema.
Name,
true);
263 Type type = FindType(name,
true);
267 type = GenericMapType.MakeGenericType(
new[] { typeof(
string), type });
271 type = GenericListType.MakeGenericType(
new [] {type});
286 ConstructorInfo ctorInfo = type.GetConstructor(Type.EmptyTypes);
287 if (ctorInfo ==
null)
288 throw new AvroException(
"Class " + name +
" has no default constructor");
290 DynamicMethod dynMethod =
new DynamicMethod(
"DM$OBJ_FACTORY_" + name, typeof(
object),
null, type,
true);
291 ILGenerator ilGen = dynMethod.GetILGenerator();
292 ilGen.Emit(OpCodes.Nop);
293 ilGen.Emit(OpCodes.Newobj, ctorInfo);
294 ilGen.Emit(OpCodes.Ret);
296 return (
CtorDelegate)dynMethod.CreateDelegate(ctorType);
312 if (!ctors.TryGetValue(key, out ctor))
314 Type type =
GetType(name, schemaType);
317 ctors.Add(key, ctor);
CtorDelegate GetConstructor(string name, Schema.Type schemaType, Type type)
Gets the default constructor for the specified type
static bool operator==(NameCtorKey left, NameCtorKey right)
override int GetHashCode()
Hash code function
static bool operator !=(NameCtorKey left, NameCtorKey right)
Schema ValueSchema
Schema for map values type
NameCtorKey(string value1, Schema.Type value2)
Base class for all schema types
IList< Schema > Schemas
List of schemas in the union
Type
Enum for schema types
override int GetHashCode()
bool Equals(NameCtorKey other)
delegate object CtorDelegate()
override bool Equals(object obj)
Class for array type schemas
Type GetType(Schema schema)
Gets the type for the specified schema
object New(string name, Schema.Type schemaType)
Creates new instance of the given type
Type GetType(string name, Schema.Type schemaType)
Gets the type of the specified type name
static ObjectCreator Instance
abstract string Name
The name of this schema.
int Count
Count of schemas in the union
Schema ItemSchema
Schema for the array 'type' attribute
Type Tag
Schema type property
Base class for all named schemas: fixed, enum, record