我在通用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)
我有一个我无法解决的场景:
我正在为mvc创建自己的自定义授权属性.我想添加的主要功能是能够更改用户重定向的位置(如果用户不在某个角色中).我不介意系统发送他们回到登录页面,如果他们没有通过身份验证,但我想选择在那里给他们,如果他们进行身份验证,但不允许访问该操作方法.
这是我想做的事情:
public class CustomAuthorizeAttribute : AuthorizeAttribute
{
public string Action;
public string Controller;
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
// if User is authenticated but not in the correct role
string url = Url.Action(this.Action, this.Controller);
httpContext.Response.Redirect(url);
}
}
Run Code Online (Sandbox Code Playgroud)
作为一个额外的奖励,我想在进行重定向之前访问ViewContext和TempData.
有关如何在属性中实例化UrlHelper和ViewContext的任何想法?
asp.net asp.net-mvc asp.net-membership custom-attributes httpcontext
可以在ASP.NET MVC 3中HttpContext访问ValidationAttribute吗?
我需要在路由数据中测试一些匹配项,以便在我的验证器上返回true.
谢谢
随着时间的推移,我们的Android项目已经扩展了很多,现在我们正在从同一个源代码树创建多个品牌的APK.由于Android的软件包命名要求,这已经变得具有挑战性.
我们在Android库项目中拥有所有共享代码,该项目包含在主应用程序项目中.此外,我们还为每个品牌的品牌资源提供了Android库"叠加"功能.当我们想要建立一个品牌时,我们会为该品牌添加一些额外的属性,最终在主要的共享代码Android库之前包含"覆盖"Android库.为了显示:
AppProject
Include: BrandALibrary or BrandBLibrary
Include: SharedLibrary
SharedLibrary
-> src/..
-> res/..
BrandALibrary
-> res/..
BrandBLibrary
-> res/..
Run Code Online (Sandbox Code Playgroud)
通过在构建时使用命令行开关包含.properties文件,可以在构建时切换"BrandALibrary或BrandBLibrary".没有什么太花哨的了.构建脚本还必须在构建时更改.APK的包名称,以便两个.APK文件可以同时在给定设备上共存(主要用于QA目的,因为我们不希望客户同时使用这两个.APK文件同时......虽然他们可以).
在我们的共享代码库项目中添加一个类之后,一直运行良好,直到我们的构建完成.我四处寻找下面这个网页的原因,我偶然发现了以下网页,该网站遇到了同样的问题并修复了他的版本以使其工作:
在构建期间,日志中会显示以下错误:
-pre-build:
-code-gen:
[echo] ----------
[echo] Handling aidl files...
[aidl] No aidl files to compile.
[echo] ----------
[echo] Handling RenderScript files...
[renderscript] No renderscript files to compile.
[echo] ----------
[echo] Handling Resources...
[aapt] Found Deleted Target File
[aapt] Generating resource IDs...
[aapt] E:\CSI\Neal\trunk\Clients\Android\MyAppSharedLib\res\layout\fancy_layout.xml:22: error: No resource identifier found for attribute 'state_add' in package 'com.myapp'
[aapt] E:\CSI\Neal\trunk\Clients\Android\MyAppSharedLib\res\layout\fancy_layout.xml:22: error: …Run Code Online (Sandbox Code Playgroud) 我正在开发一个我没有创建的ASP.NET MVC 2应用程序.在模型绑定期间修剪应用程序中的所有输入字段.但是,我想要一个NoTrim属性来阻止修剪某些字段.
例如,我有以下状态下拉字段:
<select name="State">
<option value="">Select one...</option>
<option value=" ">International</option>
<option value="AA">Armed Forces Central/SA</option>
<option value="AE">Armed Forces Europe</option>
<option value="AK">Alaska</option>
<option value="AL">Alabama</option>
...
Run Code Online (Sandbox Code Playgroud)
问题是当用户选择"国际"时,我得到验证错误,因为两个空格被修剪,State是必填字段.
这是我希望能够做到的:
[Required( ErrorMessage = "State is required" )]
[NoTrim]
public string State { get; set; }
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所获得的属性:
[AttributeUsage( AttributeTargets.Property, AllowMultiple = false )]
public sealed class NoTrimAttribute : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)
在Application_Start中设置了一个自定义模型绑定器:
protected void Application_Start()
{
ModelBinders.Binders.DefaultBinder = new MyModelBinder();
...
Run Code Online (Sandbox Code Playgroud)
以下是修剪模型活页夹的一部分:
protected override void SetProperty( ControllerContext controllerContext,
ModelBindingContext bindingContext,
PropertyDescriptor propertyDescriptor,
object value …Run Code Online (Sandbox Code Playgroud) asp.net-mvc customvalidator custom-attributes model-binding data-annotations
如果我有这样的课程
[Attr("Blah",
Data = "Blah")]
public class Test : SuperClass{}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以在运行时更改类的实例的属性值?例如在伪代码中
SuperClass test = new Test();
test.Attr.Value = "blah1";
test.Attr.Data = "blah2";
Run Code Online (Sandbox Code Playgroud)
(我有一个类的实例,我想更改属性,作为它扩展的类强制转换)
我找到了关于这个主题的这篇文章并尝试了以下内容:
public class FailerAttr : Attribute {
public FailerAttr(string s) {
throw new Exception("I should definitely fail!");
}
}
Run Code Online (Sandbox Code Playgroud)
在单元测试项目中,我有以下内容:
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class Test {
[TestMethod]
public void GoFail() {
// Make sure attribute will get initialized
new Failer();
}
private class Failer {
[FailerAttr("")]
public int Prop { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,它会成功.所以,问题是:
一些环境信息(以防万一相关):
我想了解这个特例如何运作.这是从msdn文章中解释INotifyPropertyChanged接口的解释(https://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k%28System.ComponentModel.INotifyPropertyChanged%29;k%28TargetFrameworkMoniker -.NETFramework,版本%3Dv4.5%29; k%28DevLang-csharp%29&rd = true)

正如在标记行中所说的那样,有一种方法可以拦截方法调用以替换值而不是实际的值作为参数?我想知道这样做的代码是什么样的.我知道如何使用为属性和其他类成员设置的属性,但这个用例对我来说并不清楚.
谢谢.
我正在使用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
下面我有一个解决方案,用扩展方法从字段中获取属性.现在我想用方法而不是字段做类似的事情.
public static MemberInfo GetMember<T, R>(this T instance, Expression<Func<T, R>> selector)
{
var member = selector.Body as MemberExpression;
return member?.Member;
}
public static T GetAttribute<T>(this MemberInfo meminfo) where T : Attribute
{
return meminfo.GetCustomAttributes(typeof(T)).FirstOrDefault() as T;
}
Run Code Online (Sandbox Code Playgroud)
用法:
var attr = this.GetMember(x => x.AddButtonVisibility).GetAttribute<Test>();
Run Code Online (Sandbox Code Playgroud)
所以在我的情况下,用法应该是这样的:
var attr = this.GetMethod(x => x.SomeMethod).GetAttribute<Test>();
Run Code Online (Sandbox Code Playgroud)
这有可能以任何方式或我必须尝试完全不同的东西吗?
c# ×5
asp.net-mvc ×3
.net ×2
aapt ×1
android ×1
asp.net ×1
delphi ×1
exception ×1
httpcontext ×1
inheritance ×1
linq ×1
validation ×1