我有一个自定义属性,我想应用于我的基本抽象类,以便我可以跳过在HTML中显示项目时用户不需要查看的元素.似乎覆盖基类的属性不继承属性.
重写基本属性(抽象或虚拟)是否会破坏原始属性上的属性?
从属性类定义
[AttributeUsage(AttributeTargets.Property,
Inherited = true,
AllowMultiple = false)]
public class NoHtmlOutput : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)
从抽象类定义
[NoHtmlOutput]
public abstract Guid UniqueID { get; set; }
Run Code Online (Sandbox Code Playgroud)
从混凝土类定义
public override Guid UniqueID{ get{ return MasterId;} set{MasterId = value;}}
Run Code Online (Sandbox Code Playgroud)
从类检查属性
Type t = o.GetType();
foreach (PropertyInfo pi in t.GetProperties())
{
if (pi.GetCustomAttributes(typeof(NoHtmlOutput), true).Length == 1)
continue;
// processing logic goes here
}
Run Code Online (Sandbox Code Playgroud) 是否有一种优雅的方法来获取具有自定义属性的程序集中的所有类型?
所以如果我有课
[Findable]
public class MyFindableClass
{}
Run Code Online (Sandbox Code Playgroud)
我希望能够在Assembly.GetTypes(...)返回的类型集合中找到它.
我可以用一个卑鄙的黑客来做,但我确信有人有更好的方式.
我有一些示例代码,简化了我想要完成的想法.
OptionAtrribute.cs
using System;
public class OptionAttribute : Attribute
{
public OptionAttribute(string option)
{
this.PickedOption = option;
}
public string PickedOption { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
Options.cs
using System;
public class Options
{
public const string Cat = "CT";
public const string Dog = "DG";
public const string Monkey = "MNKY";
}
Run Code Online (Sandbox Code Playgroud)
SomeClass.cs
using System;
[Option(Options.Dog)]
public class SomeClass
{
}
Run Code Online (Sandbox Code Playgroud)
如何在"SomeClass"类中获取"OptionAttribute"并获得"PickedOption"值?
更新
我不是在问如何使用反射.这是用于在保存代码的文件时生成代码.我此时没有更新的dll因此反射不起作用我试图使用Roslyn来解析实际文件.以下是我的尝试
string solutionPath = @"C:\Project\Project.sln";
var msWorkspace = MSBuildWorkspace.Create();
var solution = await msWorkspace.OpenSolutionAsync(solutionPath);
var project …Run Code Online (Sandbox Code Playgroud) 我的目标是创建一个自定义属性,如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项.
有人可以帮我弄这个吗?
谢谢.
我有一个习惯ActionFilterAttribute.为了这个问题,让我们假设它如下:
public class CustomActionFilterAttribute : ActionFilterAttribute {
public bool success { get; private set };
public override void OnActionExecuting(HttpActionContext actionContext) {
//Do something and set success
success = DoSomething(actionContext);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我的控制器用CustomActionFilter.我正在寻找的是一种方式(在我的控制器方法中)做类似的事情:
[CustomActionFilter]
public class MyController : ApiController {
public ActionResult MyAction() {
//How do I get the 'success' from my attribute?
}
}
Run Code Online (Sandbox Code Playgroud)
如果有更可接受的方式,请告诉我.
是否有一个属性告诉 json.net 忽略类的所有属性但包括所有字段(无论访问修饰符)?
如果没有,有没有办法创建一个?
基本上我想要一个装饰类的属性,它与将[JsonIgnore]每个属性放在前面具有等效的效果。
我们想要创建一个通用函数,它只选择所需的列而不是返回整个实体.例如,我有一个具有以下属性的Country类.
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CountryId { get; set; }
[Required]
public string Name { get; set; }
public int CreatedBy {get;set;}
public DateTime CreatedDate {get;set;}
Run Code Online (Sandbox Code Playgroud)
我有一个呼吸类,这对所有实体都很常见.
public class Repository<T> : IRepository<T> where T : class
{
DbContext db;
DbSet<T> currentEntity;
public Repository(DbContext db)
{
this.db = db;
currentEntity = db.Set<T>();
}
public void Add(T TEntity)
{
currentEntity.Add(TEntity);
}
public virtual List<T> GetAll()
{
return currentEntity.ToList<T>();
}
}
Run Code Online (Sandbox Code Playgroud)
因为GetAll方法返回所有列,但我想只选择Name和CountryId.如何创建只返回所需数据的通用函数?