private EasyProperty CreateInterceptorProperty(PropertyInfo propertyInfo) { EasyProperty interceptorProperty = MainTypeBuilder.CreateProperty(propertyInfo); EasyMethod getMethod = interceptorProperty.CreateGetMethod(); MethodInfo baseMethod = typeof(MulticastDelegate).GetMethod("get_Interceptor"); getMethod.CodeBuilder.AddStatement(new ReturnStatement(base.InterceptorField)); return(interceptorProperty); }
protected virtual EasyProperty[] GenerateProperties(Type inter) { PropertyInfo[] properties = inter.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); EasyProperty[] propertyArray = new EasyProperty[properties.Length]; for (int i = 0; i < properties.Length; i++) { propertyArray[i] = this.CreateProperty(properties[i]); } return(propertyArray); }
static void ProcessType(Type type, CodeTypeDeclaration ctd) { foreach (FieldInfo field in type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic)) { ctd.AddField(ConvertType(field.FieldType), field.Name).Attributes = 0; } foreach (FieldInfo field in type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic)) { EasyProperty p = ctd.AddProperty(ConvertType(field.FieldType), GetPropertyName(field.Name)); p.Getter.Return(Easy.Var(field.Name)); CodeExpression ex; if (field.FieldType.IsValueType) { ex = new CodePropertySetValueReferenceExpression(); } else { ex = GetDefaultValue("value", field); } p.Setter.Assign(Easy.Var(field.Name), ex); if (typeof(INode).IsAssignableFrom(field.FieldType)) { if (typeof(INullable).IsAssignableFrom(field.FieldType)) { p.SetStatements.Add(new CodeSnippetStatement("\t\t\t\tif (!" + field.Name + ".IsNull) " + field.Name + ".Parent = this;")); } else { p.SetStatements.Add(new CodeSnippetStatement("\t\t\t\t" + field.Name + ".Parent = this;")); } } } foreach (ConstructorInfo ctor in type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { CodeConstructor c = new CodeConstructor(); if (type.IsAbstract) { c.Attributes = MemberAttributes.Family; } else { c.Attributes = MemberAttributes.Public; } ctd.Members.Add(c); ConstructorInfo baseCtor = GetBaseCtor(type); foreach (ParameterInfo param in ctor.GetParameters()) { c.Parameters.Add(new CodeParameterDeclarationExpression(ConvertType(param.ParameterType), param.Name)); if (baseCtor != null && Array.Exists(baseCtor.GetParameters(), delegate(ParameterInfo p) { return(param.Name == p.Name); })) { continue; } c.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(GetPropertyName(param.Name)), new CodeVariableReferenceExpression(param.Name))); } if (baseCtor != null) { foreach (ParameterInfo param in baseCtor.GetParameters()) { c.BaseConstructorArgs.Add(new CodeVariableReferenceExpression(param.Name)); } } // initialize fields that were not initialized by parameter foreach (FieldInfo field in type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.NonPublic)) { if (field.FieldType.IsValueType && field.FieldType != typeof(Location)) { continue; } if (Array.Exists(ctor.GetParameters(), delegate(ParameterInfo p) { return(field.Name == p.Name); })) { continue; } c.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression(field.Name), GetDefaultValue(null, field))); } } }
public virtual CodeTypeDeclaration CreateClass(SettingsDocument setDoc) { CodeTypeDeclaration c = new CodeTypeDeclaration(setDoc.GeneratedClassName); c.AddAttribute(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute)); c.AddAttribute(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute), Easy.Prim(typeof(SettingsCodeGeneratorTool).FullName), Easy.Prim(typeof(SettingsCodeGeneratorTool).Assembly.GetName().Version.ToString())); c.TypeAttributes = TypeAttributes.NotPublic | TypeAttributes.Sealed; c.IsPartial = true; c.BaseTypes.Add(Easy.TypeRef(typeof(ApplicationSettingsBase))); CodeMemberField f = c.AddField(Easy.TypeRef(c), "defaultInstance"); f.Attributes = MemberAttributes.Private | MemberAttributes.Static; f.InitExpression = Easy.Type(typeof(ApplicationSettingsBase)) .InvokeMethod("Synchronized", Easy.New(Easy.TypeRef(c))) .CastTo(Easy.TypeRef(c)); var defaultProperty = c.AddProperty(f, "Default"); if (setDoc.UseMySettingsClassName) { c.AddAttribute(typeof(EditorBrowsableAttribute), Easy.Prim(EditorBrowsableState.Advanced)); AddAutoSaveLogic(c, defaultProperty); } foreach (SettingsEntry entry in setDoc.Entries) { Type entryType = entry.Type ?? typeof(string); SpecialSetting?specialSetting = null; foreach (SpecialTypeDescriptor desc in SpecialTypeDescriptor.Descriptors) { if (desc.type == entryType) { entryType = typeof(string); specialSetting = desc.specialSetting; break; } } EasyProperty p = c.AddProperty(entryType, entry.Name); if (entry.Scope == SettingScope.User) { p.AddAttribute(typeof(UserScopedSettingAttribute)); } else { p.AddAttribute(typeof(ApplicationScopedSettingAttribute)); } if (!string.IsNullOrEmpty(entry.Provider)) { p.AddAttribute(typeof(SettingsProviderAttribute), Easy.TypeOf(new CodeTypeReference(entry.Provider))); } if (!string.IsNullOrEmpty(entry.Description)) { p.AddAttribute(typeof(SettingsDescriptionAttribute), Easy.Prim(entry.Description)); Easy.AddSummary(p, entry.Description); } p.AddAttribute(typeof(DebuggerNonUserCodeAttribute)); if (specialSetting != null) { p.AddAttribute(typeof(SpecialSettingAttribute), Easy.Prim(specialSetting.Value)); } if (entry.GenerateDefaultValueInCode) { p.AddAttribute(typeof(DefaultSettingValueAttribute), Easy.Prim(entry.SerializedValue)); } if (entry.Scope == SettingScope.User && entry.Roaming) { p.AddAttribute(typeof(SettingsManageabilityAttribute), Easy.Prim(SettingsManageability.Roaming)); } p.Getter.Return(Easy.This.Index(Easy.Prim(entry.Name)).CastTo(entryType)); // p.GetStatements.Add(new CodeMethodReturnStatement( // new CodeCastExpression(new CodeTypeReference(entryType), // new CodeIndexerExpression(new CodeThisReferenceExpression(), // new CodePrimitiveExpression(entry.Name)) // ) // )); if (entry.Scope == SettingScope.User) { p.Setter.Assign(Easy.This.Index(Easy.Prim(entry.Name)), Easy.Value); } } return(c); }