C#使用EF访问MVC上分部类的自定义属性
我的情况如下: 我正在Visual Studio 2013上编写一个MVC网站,使用实体框架的数据库优先方法 EF自动生成模型。但我需要添加自定义属性(~不一定用于数据验证,但也用于内部流程),并通过反射访问这些自定义属性 假设我有C#使用EF访问MVC上分部类的自定义属性,c#,asp.net-mvc,entity-framework,asp.net-mvc-4,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,我的情况如下: 我正在Visual Studio 2013上编写一个MVC网站,使用实体框架的数据库优先方法 EF自动生成模型。但我需要添加自定义属性(~不一定用于数据验证,但也用于内部流程),并通过反射访问这些自定义属性 假设我有 public partial class Application { public int AppID {get; set;} public string Name {get; set;} //etc... } 我尝试了以下方法: •
public partial class Application {
public int AppID {get; set;}
public string Name {get; set;}
//etc...
}
我尝试了以下方法:
•在不同的文件上,我继续部分类:
public partial class Application {
[MyAttributeOne]
public int AppID { get; set; }
[DataType(DataType.Text)]
[MyAttributeTwo]
public string Name { get; set; }
}
•使用元数据类
public class ApplicationMetadata {
[MyAttributeOne]
public int SolutionID { get; set; }
[DataType(DataType.Text)]
[MyAttributeTwo]
public string Name { get; set; }
}
[MetadataType(typeof(ApplicationMetadata))]
public partial class Application { }
•继承具有属性的类:
public class ApplicationMetadata {
[MyAttributeOne]
public int SolutionID { get; set; }
[DataType(DataType.Text)]
[MyAttributeTwo]
public string Name { get; set; }
}
public partial class Application : ApplicationMetadata { }
private void ProcessAttributes(IEnumerable<Attribute> attributes)
{
foreach (var attr in attributes)
{
if (attr is MyAttributeOne)
{
Console.WriteLine("MyAttributeOne found");
}
else if (attr is MyAttributeTwo)
{
Console.WriteLine("MyAttributeTwo found");
}
else
{
}
}
}
•和“Buddy类”方法,我基本上使用前两种方法,但我使用“Application”类中的属性定义类
我做错什么了吗?还是这根本不可能
我需要能够使以下代码正常工作:
foreach (PropertyInfo propertyInfo in currentObject.GetType().GetProperties())
{
foreach (CustomAttributeData attrData in propertyInfo.GetCustomAttributesData())
{
if (typeof(attrData) == typeof(MyAttributeOne))
//stuff
else if (typeof(attrData) == typeof(MyAttributeTwo))
//different stuff
else
//yet more stuff
}
}
非常感谢您的关注!
问候。好的,这有点复杂,但相当简单。这真的有点像大脑的垃圾堆,但它确实有效,让你有足够的时间来处理。让我们用一些基本知识进行设置:
//A couple of custom attributes
public class MyAttributeOne : Attribute { }
public class MyAttributeTwo : Attribute { }
//A metadata class where we can use the custom attributes
public sealed class MyEntityMetadata
{
//This property has the same name as the class it is referring to
[MyAttributeOne]
public int SomeProperty { get; set; }
}
//And an entity class where we use System.ComponentModel.DataAnnotations.MetadataType
//to tell our function where the metadata is stored
[MetadataType(typeof(MyEntityMetadata))]
public class MyEntity
{
public int SomeProperty { get; set; }
}
好的,还和我在一起吗?现在,我们需要一个函数以与前面相同的方式处理属性:
public void DoStuff(object currentObject)
{
//Lets see if our entity class has associated metadata
var metaDataAttribute = currentObject.GetType()
.GetCustomAttributes()
.SingleOrDefault(a => a is MetadataTypeAttribute) as MetadataTypeAttribute;
PropertyInfo[] metaProperties = null;
//Cache the metadata properties here
if (metaDataAttribute != null)
{
metaProperties = metaDataAttribute.MetadataClassType.GetProperties();
}
//As before loop through each property...
foreach (PropertyInfo propertyInfo in currentObject.GetType().GetProperties())
{
//Refactored this out as it's called again later
ProcessAttributes(propertyInfo.GetCustomAttributes());
//Now check the metadata class
if (metaProperties != null)
{
//Look for a matching property in the metadata class
var metaPropertyInfo = metaProperties
.SingleOrDefault(p => p.Name == propertyInfo.Name);
if (metaPropertyInfo != null)
{
ProcessAttributes(metaPropertyInfo.GetCustomAttributes());
}
}
}
}
当然,这里是处理属性的重构方法:
public class ApplicationMetadata {
[MyAttributeOne]
public int SolutionID { get; set; }
[DataType(DataType.Text)]
[MyAttributeTwo]
public string Name { get; set; }
}
public partial class Application : ApplicationMetadata { }
private void ProcessAttributes(IEnumerable<Attribute> attributes)
{
foreach (var attr in attributes)
{
if (attr is MyAttributeOne)
{
Console.WriteLine("MyAttributeOne found");
}
else if (attr is MyAttributeTwo)
{
Console.WriteLine("MyAttributeTwo found");
}
else
{
}
}
}
private void ProcessAttributes(IEnumerable属性)
{
foreach(属性中的var attr)
{
if(attr是MyAttributeOne)
{
Console.WriteLine(“找到MyAttributeOne”);
}
else if(attr是MyAttributeTo)
{
Console.WriteLine(“找到我的属性”);
}
其他的
{
}
}
}
将元数据放在一个完全不同的类中如何?我已经试过了,第二个例子是不同类中的元数据,或者说是不同的文件?这会改变什么吗?我指的是一个完全不同的类,而不是实体继承的类。你能再说明一下吗?我会在我需要的代码中使用这个不同的类吗?我将如何关联DB信息(使用自动生成的类)和这个新类?