public static ClassDefinition Parse(XamlElement element, ITypeParser typeParser) { string fullName = GetClassFullName(element); if (fullName.IsNullOrEmpty()) { return null; } string baseTypeName = typeParser.ParseTypeName(element.Name); IEnumerable<MemberDefinition> members = GetMembers(element, baseTypeName, typeParser).ToArray(); return new ClassDefinition(GetTypeName(fullName), GetTypeNamespace(fullName), baseTypeName, members); }
private ITaskItem GenerateCodeFile(ITaskItem item, ITypeParser typeParser, XamlItemType itemType) { XamlElement xamlElement = XamlParser.Parse(File.ReadAllText(item.GetMetadata("FullPath"))); ClassDefinition classDefinition = XamlClassParser.Parse(xamlElement, typeParser); if (classDefinition == null) { return(null); } CodeTypeDeclaration classDeclaration = classDefinition.CreateClassDeclaration(); string itemRelativePath = item.GetRelativePath().Replace("\\", "/"); string resourceUri = String.Format("/{0};component/{1}", TargetName, itemRelativePath); classDeclaration.Members.Add(CreateInitializeComponentMethod(resourceUri)); if (itemType == XamlItemType.XamlApplicationDefinition) { classDeclaration.Members.Add(CreateEntryPointMethod(classDefinition.Name)); } CodeNamespace classNamespace = new CodeNamespace(classDefinition.Namespace); classNamespace.Types.Add(classDeclaration); CodeCompileUnit classCompileUnit = new CodeCompileUnit(); classCompileUnit.Namespaces.Add(classNamespace); string targetDirectory = Path.Combine(IntermediateOutputPath, item.GetMetadata("RelativeDir")); Directory.CreateDirectory(targetDirectory); string targetFileName = String.Format("{0}.g{1}{2}", item.GetMetadata("Filename"), item.GetMetadata("Extension"), LanguageSourceExtension); return(GenerateCodeFile(classCompileUnit, Path.Combine(targetDirectory, targetFileName))); }
public string ConvertValue(Type baseType, object value) { string str; if (value == null) { str = null; } else { if (_defaultParser.IsSupported(baseType)) { str = _defaultParser.ToRawString(value); } else { ITypeParser parser = GetParser(value.GetType()); str = parser.ToRawString(value); } } return(str); }
private static IEnumerable<MemberDefinition> GetMembers(XamlElement element, string elementTypeName, ITypeParser typeParser) { if (elementTypeName == "System.Windows.ResourceDictionary") { yield break; } XamlAttribute nameAttribute = element.Attributes.FirstOrDefault(attribute => attribute.Name == XamlLanguage.NameDirective); if (nameAttribute != null) { yield return new MemberDefinition((string)nameAttribute.Value, elementTypeName); } foreach (XamlElement child in element.Children) { string childTypeName = typeParser.ParseTypeName(child.Name.IsMemberName ? child.Name.ContainingTypeName : child.Name); foreach (MemberDefinition member in GetMembers(child, childTypeName, typeParser)) { yield return member; } } }
private static IEnumerable<MemberDefinition> GetMembers(XamlElement element, string elementTypeName, ITypeParser typeParser) { if (elementTypeName == "System.Windows.ResourceDictionary") { yield break; } XamlMember nameDirective = element.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.NameDirective); if (nameDirective != null) { yield return new MemberDefinition((string)nameDirective.GetSingleValue(), elementTypeName); } foreach (XamlElement child in element.Values.OfType<XamlElement>().Concat(element.Members.SelectMany(member => member.Values.OfType<XamlElement>()))) { string childTypeName = typeParser.ParseTypeName(child.Name); foreach (MemberDefinition member in GetMembers(child, childTypeName, typeParser)) { yield return member; } } }
/// <summary> /// Gets the Type parser. /// </summary> /// <param name="typeKeyword">The Type keyword.</param> /// <returns>The Type parser.</returns> private static ITypeParser GetTypeParser(string typeKeyword) { if (!string.IsNullOrEmpty(typeKeyword)) { if (cachedTypeParsersMap == null) { cachedTypeParsersMap = new Dictionary <Type, ITypeParser>(); } Type type = TypeParserFactory.GetTypeParserType(typeKeyword); ITypeParser typeParser = null; if (type != null) { if (cachedTypeParsersMap.ContainsKey(type)) { typeParser = cachedTypeParsersMap[type]; if (typeParser == null) { typeParser = TypeParserFactory.CreateTypeParser(typeKeyword); cachedTypeParsersMap[type] = typeParser; } } else { typeParser = TypeParserFactory.CreateTypeParser(typeKeyword); cachedTypeParsersMap.Add(type, typeParser); } return(typeParser); } } return(null); }
private void AddParser(List <ParserAndType> parserList, Type type, ITypeParser typeParser) { var record = new ParserAndType() { Type = type, Parser = typeParser, }; var idx = parserList.FindIndex(r => r.Type == type); if (idx != -1) { parserList[idx] = record; } else { parserList.Add(record); } if (parserList == _ParserList) { SetDirectAccessProperty(type, typeParser); } }
/// <summary> /// Creates a new object. /// </summary> /// <param name="parserTypes"></param> public UseParserAttribute(params Type[] parserTypes) { var parsersList = parserTypes .Select(r => { ITypeParser parserInstance = null; try { parserInstance = Activator.CreateInstance(r) as ITypeParser; } catch { } return(parserInstance); }) .ToArray(); if (parsersList.Any(r => r == null)) { CtorErrorMessage = $"At least one of the parser types does not implement {nameof(ITypeParser)}"; } var countDistinctTypes = parsersList .Where(r => r != null) .Select(r => r .GetType() .GetInterface(TypeParserResolver.ITypeParserGenericName) .GetGenericArguments()[0] ) .Distinct(); if (CtorErrorMessage == null && countDistinctTypes.Count() != parsersList.Length) { CtorErrorMessage = $"At least two of the parsers are for the same type"; } CtorErrorMessage = CtorErrorMessage ?? ""; IsValid = CtorErrorMessage == ""; Parsers = IsValid ? parsersList : new ITypeParser[0]; }
public NullableTypeParser(ITypeParser <T> nonNullableTypeParser) { _nonNullableTypeParser = nonNullableTypeParser; }
private ITaskItem GenerateCodeFile(ITaskItem item, ITypeParser typeParser, XamlItemType itemType) { XamlElement xamlElement = XamlParser.Parse(File.ReadAllText(item.GetMetadata("FullPath"))); ClassDefinition classDefinition = XamlClassParser.Parse(xamlElement, typeParser); if (classDefinition == null) { return null; } CodeTypeDeclaration classDeclaration = classDefinition.CreateClassDeclaration(); string itemRelativePath = item.GetRelativePath().Replace("\\", "/"); string resourceUri = String.Format("/{0};component/{1}", TargetName, itemRelativePath); classDeclaration.Members.Add(CreateInitializeComponentMethod(resourceUri)); if (itemType == XamlItemType.XamlApplicationDefinition) { classDeclaration.Members.Add(CreateEntryPointMethod(classDefinition.Name)); } CodeNamespace classNamespace = new CodeNamespace(classDefinition.Namespace); classNamespace.Types.Add(classDeclaration); CodeCompileUnit classCompileUnit = new CodeCompileUnit(); classCompileUnit.Namespaces.Add(classNamespace); string targetDirectory = Path.Combine(IntermediateOutputPath, item.GetMetadata("RelativeDir")); Directory.CreateDirectory(targetDirectory); string targetFileName = String.Format("{0}.g{1}{2}", item.GetMetadata("Filename"), item.GetMetadata("Extension"), LanguageSourceExtension); return GenerateCodeFile(classCompileUnit, Path.Combine(targetDirectory, targetFileName)); }
public static ITypeParser CreateNullableTypeParser(Type nullableType, ITypeParser typeParser) => typeof(NullableTypeParser <>).MakeGenericType(nullableType).GetConstructors()[0].Invoke(new object[] { typeParser }) as ITypeParser;
/// <summary> /// Using this method user could modify entity code generation options: /// <list type="bullet"> /// <item>modify associated table/view metadata descriptor: <see cref="EntityModel.Metadata"/></item> /// <item>modify entity class code generation options including custom attributes: <see cref="EntityModel.Class"/></item> /// <item>modify/add/remove data context table access property: <see cref="EntityModel.ContextProperty"/></item> /// <item>edit list of generated Find/FindAsync/FindQuery extensions: <see cref="EntityModel.FindExtensions"/></item> /// <item>modify column list: <see cref="EntityModel.Columns"/> including column metadata and property code generation options.</item> /// </list> /// </summary> /// <param name="typeParser">Type parser service to create type tokens.</param> /// <param name="entityModel">Entity model descriptor.</param> public virtual void PreprocessEntity(ITypeParser typeParser, EntityModel entityModel) { }
/// <summary> /// Using this method user could modify association code generation options: /// <list type="bullet"> /// <item>Metadata for both sides of association: <see cref="AssociationModel.SourceMetadata"/> and <see cref="AssociationModel.TargetMetadata"/></item> /// <item>Configure association properties in entity classes: <see cref="AssociationModel.Property"/> and <see cref="AssociationModel.BackreferenceProperty"/></item> /// <item>Configure association extension methods: <see cref="AssociationModel.Extension"/> and <see cref="AssociationModel.BackreferenceExtension"/></item> /// <item>Association cardinality: <see cref="AssociationModel.ManyToOne"/></item> /// </list> /// Also it is possible to modify set of columns, used by association, but it is probably not very useful. /// </summary> /// <param name="typeParser">Type parser service to create type tokens.</param> /// <param name="associationModel">Association model descriptor.</param> public virtual void PreprocessAssociation(ITypeParser typeParser, AssociationModel associationModel) { }
/// <summary> /// Initializes a new instance of the <see cref="TypeBuilder"/> class. /// </summary> /// <param name="typeConstructor">Type constructor</param> /// <param name="typeFilter">Type filter</param> /// <param name="typeParser">Type parser</param> /// <param name="typeResolver">Type resolver</param> public TypeBuilder(ITypeActivator typeActivator, ITypeConstructor typeConstructor, ITypeFilter typeFilter, ITypeParser typeParser, ITypeResolver typeResolver, ITypeDependencyAttributeProvider typeDependencyAttributeProvider = null, ITypeInjectionAttributeProvider typeInjectionAttributeProvider = null) { DependencyAttributeExtractor = typeDependencyAttributeProvider ?? throw new ArgumentNullException(nameof(typeDependencyAttributeProvider)); InjectionAttributeExtractor = typeInjectionAttributeProvider ?? throw new ArgumentNullException(nameof(typeInjectionAttributeProvider)); Activator = typeActivator ?? throw new ArgumentNullException(nameof(typeActivator)); Constructor = typeConstructor ?? throw new ArgumentNullException(nameof(typeConstructor)); Filter = typeFilter ?? throw new ArgumentNullException(nameof(typeFilter)); Resolver = typeResolver ?? throw new ArgumentNullException(nameof(typeResolver)); Parser = typeParser ?? throw new ArgumentNullException(nameof(typeParser)); }
/// <summary> /// Creates a new instance of <see cref="AttributeMapperStrategy"/> with the specified <paramref name="annotatedPropertyValidator"/> and <paramref name="typeParser"/>. /// </summary> /// <param name="annotatedPropertyValidator">An instance of <see cref="IAnnotatedPropertyValidator"/>.</param> /// <param name="typeParser">An instance of <see cref="ITypeParser"/>.</param> public AttributeMapperStrategy([NotNull] IAnnotatedPropertyValidator annotatedPropertyValidator, [NotNull] ITypeParser typeParser) { _typeParser = typeParser ?? throw new ArgumentNullException(nameof(typeParser)); _annotatedPropertyValidator = annotatedPropertyValidator ?? throw new ArgumentNullException(nameof(annotatedPropertyValidator)); }
/// <summary> /// Creates a new instance of <see cref="AttributeMapperStrategy"/> with a <see cref="AnnotatedPropertyValidator"/> using the specified <paramref name="inspector"/> and <paramref name="typeParser"/>. /// </summary> /// <param name="inspector">An instance of <see cref="IPropertyInspector"/>.</param> /// <param name="typeParser">An instance of <see cref="ITypeParser"/>.</param> public AttributeMapperStrategy([NotNull] IPropertyInspector inspector, [NotNull] ITypeParser typeParser) : this(new AnnotatedPropertyValidator(inspector), typeParser) { }
public MarkupExtensionTypeParser(ITypeParser typeParser) { this.typeParser = typeParser; }
public override void Given() { TypeParser = new TypeParser(); }
public AutoBuilder WithTypeParser <TOutput>(ITypeParser <string, TOutput> typeParser) { typeParsers.Add(typeof(TOutput), new ObjectTypeParserWrapper <string, TOutput>(typeParser)); return(this); }
public void RegisterTypeParser <T>(ITypeParser parser) { TypeParsers.RegisterTypeParser <T>(parser); }
/// <summary> /// Using this method user could modify scalar function code generation options: /// <list type="bullet"> /// <item>Return scalar value or tuple descriptor: <see cref="ScalarFunctionModel.Return"/> or <see cref="ScalarFunctionModel.ReturnTuple"/></item> /// <item>Function metadata: <see cref="ScalarFunctionModelBase.Metadata"/></item> /// <item>Method code-generation options: <see cref="FunctionModelBase.Method"/></item> /// <item>Parameters: <see cref="FunctionModelBase.Parameters"/></item> /// </list> /// </summary> /// <param name="typeParser">Type parser service to create type tokens.</param> /// <param name="functionModel">Function model descriptor.</param> public virtual void PreprocessScalarFunction(ITypeParser typeParser, ScalarFunctionModel functionModel) { }
/// <summary> /// Using this method user could modify aggregate function code generation options: /// <list type="bullet"> /// <item>Return type: <see cref="AggregateFunctionModel.ReturnType"/></item> /// <item>Function metadata: <see cref="ScalarFunctionModelBase.Metadata"/></item> /// <item>Method code-generation options: <see cref="FunctionModelBase.Method"/></item> /// <item>Parameters: <see cref="FunctionModelBase.Parameters"/></item> /// </list> /// </summary> /// <param name="typeParser">Type parser service to create type tokens.</param> /// <param name="functionModel">Function model descriptor.</param> public virtual void PreprocessAggregateFunction(ITypeParser typeParser, AggregateFunctionModel functionModel) { }
public ObjectTypeParserWrapper(ITypeParser <TInput, TOutput> typeParser) { this.typeParser = typeParser; }
/// <summary> /// Using this method you can specify which .NET type and <see cref="DataType"/> enum value to use with specific database type. /// Method called only once per database type. /// <see cref="TypeMapping.CLRType"/> shouldn't be a nullable type, as nullability applied to it later automatically based on owning object (e.g. column or procedure parameter) nullability. /// </summary> /// <param name="databaseType">Database type specification.</param> /// <param name="typeParser">Type parser to create value for <see cref="TypeMapping.CLRType"/> property from <see cref="Type"/> instance or type name string.</param> /// <param name="defaultMapping">Default type mapping for specified <paramref name="databaseType"/>.</param> /// <returns>Type mapping information for specified <paramref name="databaseType"/>.</returns> public virtual TypeMapping?GetTypeMapping(DatabaseType databaseType, ITypeParser typeParser, TypeMapping?defaultMapping) => defaultMapping;
private static IEnumerable <MemberDefinition> GetMembers(XamlElement element, string elementTypeName, ITypeParser typeParser) { if (elementTypeName == "System.Windows.ResourceDictionary") { yield break; } XamlMember nameDirective = element.Directives.FirstOrDefault(directive => directive.Name == XamlLanguage.NameDirective); if (nameDirective != null) { yield return(new MemberDefinition((string)nameDirective.GetSingleValue(), elementTypeName)); } foreach (XamlElement child in element.Values.OfType <XamlElement>().Concat(element.Members.SelectMany(member => member.Values.OfType <XamlElement>()))) { string childTypeName = typeParser.ParseTypeName(child.Name); foreach (MemberDefinition member in GetMembers(child, childTypeName, typeParser)) { yield return(member); } } }
public void SetupParserFor <T>(ITypeParser parser) { SetupCustomBinder(new PrimitiveBinder <T>(parser)); }
public override void Given() { Parser = TypeParser.Default; }
private void SetDirectAccessProperty(Type type, ITypeParser typeParser) { if (type == typeof(bool)) { BoolParser = (ITypeParser <bool>)typeParser; } else if (type == typeof(byte)) { ByteParser = (ITypeParser <byte>)typeParser; } else if (type == typeof(char)) { CharParser = (ITypeParser <char>)typeParser; } else if (type == typeof(Int16)) { Int16Parser = (ITypeParser <Int16>)typeParser; } else if (type == typeof(UInt16)) { UInt16Parser = (ITypeParser <UInt16>)typeParser; } else if (type == typeof(Int32)) { Int32Parser = (ITypeParser <Int32>)typeParser; } else if (type == typeof(UInt32)) { UInt32Parser = (ITypeParser <UInt32>)typeParser; } else if (type == typeof(Int64)) { Int64Parser = (ITypeParser <Int64>)typeParser; } else if (type == typeof(UInt64)) { UInt64Parser = (ITypeParser <UInt64>)typeParser; } else if (type == typeof(float)) { FloatParser = (ITypeParser <float>)typeParser; } else if (type == typeof(double)) { DoubleParser = (ITypeParser <double>)typeParser; } else if (type == typeof(decimal)) { DecimalParser = (ITypeParser <decimal>)typeParser; } else if (type == typeof(DateTime)) { DateTimeParser = (ITypeParser <DateTime>)typeParser; } else if (type == typeof(DateTimeOffset)) { DateTimeOffsetParser = (ITypeParser <DateTimeOffset>)typeParser; } else if (type == typeof(Guid)) { GuidParser = (ITypeParser <Guid>)typeParser; } else if (type == typeof(byte[])) { ByteArrayParser = (ITypeParser <byte[]>)typeParser; } else if (type == typeof(string)) { StringParser = (ITypeParser <string>)typeParser; } }
public AttributeMapperStrategy(IPropertyInspector inspector, ITypeParser typeParser) { _inspector = inspector; _typeParser = typeParser; }
/// <summary> /// Using this method user could modify stored procedure code generation options: /// <list type="bullet"> /// <item>Return parameter descriptor: <see cref="StoredProcedureModel.Return"/></item> /// <item>Return tables (data sets) descriptor: <see cref="StoredProcedureModel.Results"/></item> /// <item>Error, returned by data set schema load procedure: <see cref="TableFunctionModelBase.Error"/></item> /// <item>Metadata (procedure name): <see cref="TableFunctionModelBase.Name"/></item> /// <item>Method code-generation options: <see cref="FunctionModelBase.Method"/></item> /// <item>Parameters: <see cref="FunctionModelBase.Parameters"/></item> /// </list> /// </summary> /// <param name="typeParser">Type parser service to create type tokens.</param> /// <param name="procedureModel">Stored procedure model descriptor.</param> public virtual void PreprocessStoredProcedure(ITypeParser typeParser, StoredProcedureModel procedureModel) { }
public MyContainer(IConfigStore store, ITypeParser customParser = null) : base("MyApp") { _store = store; _customParser = customParser; }
/// <summary> /// Using this method user could modify table function code generation options: /// <list type="bullet"> /// <item>Function metadata: <see cref="TableFunctionModel.Metadata"/></item> /// <item>Return table descriptor: <see cref="TableFunctionModel.Result"/></item> /// <item>Error, returned by data set schema load procedure: <see cref="TableFunctionModelBase.Error"/></item> /// <item>Metadata (function name): <see cref="TableFunctionModelBase.Name"/></item> /// <item>Method code-generation options: <see cref="FunctionModelBase.Method"/></item> /// <item>Parameters: <see cref="FunctionModelBase.Parameters"/></item> /// </list> /// </summary> /// <param name="typeParser">Type parser service to create type tokens.</param> /// <param name="functionModel">Function model descriptor.</param> public virtual void PreprocessTableFunction(ITypeParser typeParser, TableFunctionModel functionModel) { }
public static ITypeParser CreateNullableTypeParser(Type nullableType, ITypeParser typeParser) => Activator.CreateInstance(typeof(NullableTypeParser <>).MakeGenericType(nullableType), new object[] { typeParser }) as ITypeParser;
public ParametersList(MethodInfo methodInfo) { List <Parameter> args = new List <Parameter>(); bool hasContextParameter = false; foreach (ParameterInfo param in methodInfo.GetParameters()) { if (param.IsOptional) { throw new UnsupportedOptionalParameterException(methodInfo, param); } bool isContext = false; if (param.ParameterType.IsAssignableFrom(typeof(ICommandContext))) { hasContextParameter = true; isContext = true; } FlagAttribute flagAttribute = param.GetCustomAttribute <FlagAttribute>(false); NameAttribute nameAttribute = param.GetCustomAttribute <NameAttribute>(false); if (flagAttribute != null && param.ParameterType != typeof(bool)) { throw new UnsupportedNonBooleanFlagException(methodInfo, param); } Type checkType = param.ParameterType; if (param.ParameterType.IsArray || (param.ParameterType.IsGenericType && param.ParameterType.GetGenericTypeDefinition() == typeof(List <>))) { checkType = param.ParameterType.GetElementType() ?? param.ParameterType.GetGenericArguments()[0]; } if (checkType != typeof(ICommandContext)) { ITypeParser parser = Parser.GetParserForType(checkType); if (parser == null) { throw new UnconvertableTypeParameterException(methodInfo, param); } } if (flagAttribute == null && !isContext) { RequiredParamterCount++; } args.Add(new Parameter( name: nameAttribute?.Name, parameterInfo: param, isFlag: flagAttribute != null, shortName: flagAttribute?.ShortName, longName: flagAttribute?.LongName, isParams: param.IsDefined(typeof(ParamArrayAttribute), false), isContext: isContext, defaultValue: flagAttribute?.DefaultValue ?? false)); } // Throw exception if a ICommandContext parameter isnt found if (!hasContextParameter) { throw new MissingContextParameterException(methodInfo); } Parameters = args; Syntax = GenerateSyntax(); }