我在通用DataGrids中显示Business Object,我想通过自定义属性设置列标题,如:
class TestBo
{
[Header("NoDisp")]
public int ID {get; set;}
[Header("Object's name")]
public String Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这么好,但我还想通过继承将我的显示与我的数据分开:
class TestBO
{
public int ID {get; set;}
public String Name { get; set; }
}
class TestPresentationBO : TestBO
{
//Question: how to simply set the Header attribute on the different properties?
}
Run Code Online (Sandbox Code Playgroud)
我通过Child构造函数中的SetCustomAttribute通过反射看到了一个解决方案,但它会很麻烦,所以这个问题有一个简单而优雅的技巧吗?
请阻止我打破数据/演示文稿的分离; o)
在这种情况下,我们在复选框上有一个自定义属性,名为data-ref.
如何获得价值.这没用.
this.attr('data-ref');
Run Code Online (Sandbox Code Playgroud)
有任何想法吗,
奇妙
我正在设计一个自定义属性类.
public class MyAttr: Attribute
{
public ValueRange ValRange { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我试图将此属性分配给相邻类中的属性:
public class Foo
{
[MyAttr(ValRange= new ValueRange())]
public string Prop { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但是,编译器抱怨以下内容:
'ValRange'不是有效的命名属性参数,因为它不是有效的属性参数类型
我也尝试将ValueRange类转换为a struct,希望成为值类型可以解决问题.有没有办法解决?
有没有办法访问属性中附加属性的类和属性名称?
例如
public class User {
public string Email { get; set; }
public string FirstName { get; set; }
[MyAttribute]
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后在MyAttribute类中
public class MyAttributeAttribute {
public MyAttributeAttribute () : base() {
string className = /*GET CLASS NAME - should return "User" */
string propertyName = /*GET PROPERTY NAME - should return LastName*/
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以在构造函数中传递信息,但是希望有一种简单的方法可以通过反射或者反复来节省一遍又一遍的重新输入信息.
我正在使用Delphi 10 Seattle处理VCL应用程序,并且当我注意到Delphi Ref为该Rect参数添加了自定义属性时,通过IDE创建了一个TDBGrid事件处理程序:
procedure TfrmXxx.yyyDrawColumnCell(Sender: TObject;
const [Ref] Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
//
end;
Run Code Online (Sandbox Code Playgroud)
更新
delphi custom-attributes parameter-passing pass-by-reference
我AuthorizeAttribute在遗留MVC5项目中有一个自定义:
public class AuthorizeWithLoggingAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (!base.AuthorizeCore(httpContext)) {Log(FilterContext);}
}
}
Run Code Online (Sandbox Code Playgroud)
我们在查看日志时注意到,除了要应用于控制器之外[AuthorizeWithLogging],它还在代码中的其他位置显式调用,生成虚假日志:
var filters = new FilterInfo(FilterProviders.Providers.GetFilters(controllerContext, actionDescriptor));
foreach (var authFilter in filters.AuthorizationFilters)
{
authFilter.OnAuthorization(authContext);
if (authContext.Result != null) {return false;}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉(通过StackTrace什么)OnAuthorization方法是显式调用,还是从属性调用?我目前最好的是
Environment.StackTrace.Contains("at System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters").
我们正在整个应用程序中使用Ajax调用 - 尝试在尝试执行任何Ajax请求时,如果会话已经过期,则尝试找到重定向到登录页面的全局解决方案.我编写了以下解决方案,从这篇文章中获取帮助 - 在ajax调用中处理会话超时
不确定为什么在我关心的事件中"HandleUnauthorizedRequest"没有被解雇.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class CheckSessionExpireAttribute :AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
var url = new UrlHelper(filterContext.RequestContext);
var loginUrl = url.Content("/Default.aspx");
filterContext.HttpContext.Session.RemoveAll();
filterContext.HttpContext.Response.StatusCode = 403;
filterContext.HttpContext.Response.Redirect(loginUrl, false);
filterContext.Result = new EmptyResult();
}
else
{
base.HandleUnauthorizedRequest(filterContext);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在控制器操作中使用Above自定义属性如下:
[NoCache]
[CheckSessionExpire]
public ActionResult GetSomething()
{
}
Run Code Online (Sandbox Code Playgroud)
AJAX Call(JS部分):
function GetSomething()
{
$.ajax({
cache: false,
type: "GET",
async: true,
url: "/Customer/GetSomething",
success: function (data) {
},
error: function (xhr, …Run Code Online (Sandbox Code Playgroud) 我创建了一个自定义属性来装饰我想在运行时查询的许多类:
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
public class ExampleAttribute : Attribute
{
public ExampleAttribute(string name)
{
this.Name = name;
}
public string Name
{
get;
private set;
}
}
Run Code Online (Sandbox Code Playgroud)
这些类中的每一个都派生自一个抽象基类:
[Example("BaseExample")]
public abstract class ExampleContentControl : UserControl
{
// class contents here
}
public class DerivedControl : ExampleContentControl
{
// class contents here
}
Run Code Online (Sandbox Code Playgroud)
我是否需要将此属性放在每个派生类上,即使我将其添加到基类中?该属性被标记为可继承,但是当我执行查询时,我只看到基类而不是派生类.
从另一个线程:
var typesWithMyAttribute =
from a in AppDomain.CurrentDomain.GetAssemblies()
from t in a.GetTypes()
let attributes = t.GetCustomAttributes(typeof(ExampleAttribute), true)
where attributes != null && attributes.Length > 0 …Run Code Online (Sandbox Code Playgroud) C#中的属性可以与集合初始值设定项一起使用吗?
例如,我想做类似以下的事情:
[DictionaryAttribute(){{"Key", "Value"}, {"Key", "Value"}}]
public class Foo { ... }
Run Code Online (Sandbox Code Playgroud)
我知道属性可以有命名参数,因为这看起来与对象初始化器非常相似,我想知道集合初始化器是否也可用.
c# attributes custom-attributes object-initializers collection-initializer
我的目标是创建一个自定义属性,如System.ComponentModel.DataAnnotations.Display,它允许我传递一个参数.
例如:在System.ComponentModel.DataAnnotations.Display中,我可以将值传递给参数Name
[Display(Name = "PropertyName")]
public int Property { get; set; }
Run Code Online (Sandbox Code Playgroud)
我想做同样的事情,但在控制器和行动如下
[CustomDisplay(Name = "Controller name")]
public class HomeController : Controller
Run Code Online (Sandbox Code Playgroud)
然后使用其值填充ViewBag或ViewData项.
有人可以帮我弄这个吗?
谢谢.
c# ×7
asp.net-mvc ×2
attributes ×2
inheritance ×2
reflection ×2
.net-3.5 ×1
ajax-request ×1
checkbox ×1
delphi ×1
forms ×1
jquery ×1