private void InitializeField(FieldBuilder fieldBuilder, ILGenerator ilConstructor, SampleProperty sampleProperty) { if (ilConstructor == null) { return; } if (!sampleProperty.IsBasicType) { SampleDataDesignTimeTypeGenerator.TypeBuildState typeBuildState = this.buildStates[(SampleNonBasicType)sampleProperty.PropertySampleType]; ilConstructor.Emit(OpCodes.Ldarg_0); ilConstructor.Emit(OpCodes.Newobj, (ConstructorInfo)typeBuildState.ConstructorBuilder); ilConstructor.Emit(OpCodes.Stfld, (FieldInfo)fieldBuilder); } else { SampleBasicType sampleBasicType = (SampleBasicType)sampleProperty.PropertySampleType; if (sampleBasicType == SampleBasicType.String) { FieldInfo field = typeof(string).GetField("Empty", BindingFlags.Static | BindingFlags.Public); ilConstructor.Emit(OpCodes.Ldarg_0); ilConstructor.Emit(OpCodes.Ldsfld, field); ilConstructor.Emit(OpCodes.Stfld, (FieldInfo)fieldBuilder); } else if (sampleBasicType == SampleBasicType.Date) { PropertyInfo property = typeof(DateTime).GetProperty("Today", BindingFlags.Static | BindingFlags.Public); ilConstructor.Emit(OpCodes.Ldarg_0); ilConstructor.Emit(OpCodes.Call, property.GetGetMethod()); ilConstructor.Emit(OpCodes.Stfld, (FieldInfo)fieldBuilder); } else if (sampleBasicType == SampleBasicType.Number) { ilConstructor.Emit(OpCodes.Ldarg_0); ilConstructor.Emit(OpCodes.Ldc_R8, 0.0); ilConstructor.Emit(OpCodes.Stfld, (FieldInfo)fieldBuilder); } else if (sampleBasicType == SampleBasicType.Boolean) { ilConstructor.Emit(OpCodes.Ldarg_0); ilConstructor.Emit(OpCodes.Ldc_I4, 0); ilConstructor.Emit(OpCodes.Stfld, (FieldInfo)fieldBuilder); } else { if (sampleBasicType != SampleBasicType.Image) { return; } ilConstructor.Emit(OpCodes.Ldarg_0); ilConstructor.Emit(OpCodes.Ldnull); ilConstructor.Emit(OpCodes.Stfld, (FieldInfo)fieldBuilder); } } }
private Type GenerateCollectionType(SampleCollectionType collectionType, SampleDataDesignTimeTypeGenerator.TypeBuildState buildState) { collectionType.DesignTimeType = buildState.TypeBuilder.UnderlyingSystemType; Type type = this.dataSet.ProjectContext.ResolveType(PlatformTypes.ObservableCollection).RuntimeType.MakeGenericType(this.GetOrGenerateDesignTimeType(collectionType.ItemSampleType)); ILGenerator ilGenerator = buildState.ConstructorBuilder.GetILGenerator(); ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Call, constructor); ilGenerator.Emit(OpCodes.Ret); return(this.GenerateDesignTimeType(buildState.TypeBuilder)); }
private Type GenerateCompositeType(SampleCompositeType compositeType, SampleDataDesignTimeTypeGenerator.TypeBuildState buildState) { compositeType.DesignTimeType = buildState.TypeBuilder.UnderlyingSystemType; ILGenerator ilGenerator = buildState.ConstructorBuilder.GetILGenerator(); ConstructorInfo constructor = compositeType.DesignTimeType.BaseType.GetConstructor(Type.EmptyTypes); ilGenerator.Emit(OpCodes.Ldarg_0); ilGenerator.Emit(OpCodes.Call, constructor); foreach (SampleProperty sampleProperty in (IEnumerable <SampleProperty>)compositeType.SampleProperties) { this.GenerateProperty(buildState.TypeBuilder, sampleProperty, ilGenerator); } ilGenerator.Emit(OpCodes.Ret); return(this.GenerateDesignTimeType(buildState.TypeBuilder)); }
private Type GetOrBeginDesignTimeType(SampleType sampleType) { Type type; if (!sampleType.IsBasicType) { SampleNonBasicType key = (SampleNonBasicType)sampleType; SampleDataDesignTimeTypeGenerator.TypeBuildState typeBuildState = (SampleDataDesignTimeTypeGenerator.TypeBuildState)null; if (!this.buildStates.TryGetValue(key, out typeBuildState)) { typeBuildState = !sampleType.IsCollection ? this.BeginCompositeType((SampleCompositeType)sampleType) : this.BeginCollectionType((SampleCollectionType)sampleType); this.buildStates[key] = typeBuildState; } type = typeBuildState.TypeBuilder.UnderlyingSystemType; } else { type = this.dataSet.ResolveSampleType(sampleType).RuntimeType; } return(type); }
private Type GetOrGenerateDesignTimeType(SampleType sampleType) { Type type; if (!sampleType.IsBasicType) { SampleNonBasicType index = (SampleNonBasicType)sampleType; SampleDataDesignTimeTypeGenerator.TypeBuildState buildState = this.buildStates[index]; if (!buildState.IsGenerated) { buildState.IsGenerated = true; index.DesignTimeType = !sampleType.IsCollection ? this.GenerateCompositeType((SampleCompositeType)sampleType, buildState) : this.GenerateCollectionType((SampleCollectionType)sampleType, buildState); } type = index.DesignTimeType; } else { type = this.dataSet.ResolveSampleType(sampleType).RuntimeType; } return(type); }