private void MapAuthorize(TypeBuilder builder, Type srcType) { // special case, when authorization is disabled if (!_options.Authorization) { builder.AddAttribute(typeof(AspNetCoreAttr.AllowAnonymousAttribute)); return; } bool hasAuthorizeAttr = GetAttributes <AuthorizeAttribute>(srcType).Any(); bool hasAnonymousAttr = GetAttributes <AllowAnonymousAttribute>(srcType).Any(); if (hasAnonymousAttr) { builder.AddAttribute(typeof(AspNetCoreAttr.AllowAnonymousAttribute)); } else if (hasAuthorizeAttr) { MapAttribute <AuthorizeAttribute>(srcType, (attr) => { var permissions = attr.Permissions; if (permissions == null || !permissions.Any()) { builder.AddAttribute(typeof(AspNetCoreAttr.AuthorizeAttribute)); } else { builder.AddAttribute(typeof(AuthorizePermissionsAttribute), new object[] { permissions }); } }); } else { builder.AddAttribute(typeof(AspNetCoreAttr.AuthorizeAttribute)); } }
private void MapExpose(TypeBuilder builder, CqrsInfo info) { MapAttribute <ExposeAttribute>(info.ReqType, (attr) => { var uri = _options.GetUrlPath(info); builder.AddAttribute(typeof(Microsoft.AspNetCore.Mvc.RouteAttribute), new object[] { uri }); builder.AddAttribute(typeof(OutputFormatterAttribute), new object[1] { attr.Formatter ?? "" }); }); }
private static void AddTypeAttributes(TypeBuilder typeBuilder, TypeSpecification typeSpec) { foreach (var attributeTemplate in typeSpec.Attributes) { if (typeSpec.BeforeAttributeIsAddedToType != null) { typeSpec.BeforeAttributeIsAddedToType(attributeTemplate.Template, typeBuilder); } typeBuilder.AddAttribute(attributeTemplate); } }
private void CopyAttributes(TypeBuilder builder, Type srcType) { // copy only not listed attributes with default 0 params constructor var attrs = srcType.GetCustomAttributes().Where(x => !_copyAttributesIgnoreList.Contains(x.GetType())). Where(x => x.GetType().GetConstructors().Any(y => y.GetParameters().Length == 0)); attrs.ForEach(attr => { var props = attr.GetType().GetProperties().Where(x => x.CanWrite). ToDictionary(x => x.Name, x => x.GetValue(attr)); builder.AddAttribute(attr.GetType(), null, props); }); }
private void MapResponseType(TypeBuilder builder, Type responseType) { // response type if (responseType != null) { // special case for IQueryable responses (breeze case) if (_options.QueryResultMode && responseType.IsGenericType && responseType.GetGenericTypeDefinition() == typeof(IQueryable <>)) { var itemType = responseType.GetGenericArguments()[0]; responseType = typeof(QueryResult <>).MakeGenericType(itemType); } // set response type builder.AddAttribute( typeof(Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute), new object[] { responseType, Microsoft.AspNetCore.Http.StatusCodes.Status200OK }); } // validation errors builder.AddAttribute( typeof(Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute), new object[] { typeof(ValidationErrorResponse), Microsoft.AspNetCore.Http.StatusCodes.Status400BadRequest }); }
public static Type GetIdEntity <TProperty>(string strTableName) { TypeBuilder typeBuilder = DynamicTypeHelper.BuildType("IdEntity", "Entitys", "SyncJob.Domain"); //定义构造器参数 Type[] ctorParams = new Type[] { typeof(string) }; object[] ctorParamValues = new object[] { $"`#Temp`{strTableName}" }; typeBuilder.AddAttribute <TableAttribute>(ctorParams, ctorParamValues); var id = typeBuilder.AddProperty <TProperty>("Id"); typeBuilder.AddCtor(new Type[] { typeof(TProperty) }, new FieldBuilder[] { id }); //return typeBuilder.CreateType(); return(null); }