Get the attributes from the interface methods and the class methods - c#

Whats the best approach for getting the attribute values from a classes methods and from the interface methods when the methods are overloaded?
For example I would want to know that in the following example the Get method with one parameter has the two attributes and the values are 5 and "any" while the other method has attributes with values 7 and "private".
public class ScopeAttribute : System.Attribute
{
public string Allowed { get; set; }
}
public class SizeAttribute : System.Attribute
{
public int Max { get; set; }
}
public interface Interface1
{
[SizeAttribute( Max = 5 )]
string Get( string name );
[SizeAttribute( Max = 7 )]
string Get( string name, string area );
}
public class Class1 : Interface1
{
[ScopeAttribute( Allowed = "any" )]
public string Get( string name )
{
return string.Empty;
}
[ScopeAttribute( Allowed = "private" )]
public string Get( string name, string area )
{
return string.Empty;
}
}

The only way I found was to check what interfaces the class implements and check attributes of the properties (if any exist) on those interfaces:
static bool HasAttribute (PropertyInfo property, string attribute) {
if (property == null)
return false;
if (GetCustomAttributes ().Any (a => a.GetType ().Name == attribute))
return true;
var interfaces = property.DeclaringType.GetInterfaces ();
for (int i = 0; i < interfaces.Length; i++)
if (HasAttribute (interfaces[i].GetProperty (property.Name), attribute))
return true;
return false;
}
You can probably adopt it to methods equally easy.
Note: overall approach is tested but the code itself is ad-hoc and may not compile.

You can use TypeDescriptor API
System.ComponentModel.TypeDescriptor.GetAttributes(object)

You should use reflection to get the custom attributes values
use MemberInfo.GetCustomAttributes Method to return the custom attributes attached to your member
here is a tutorial http://msdn.microsoft.com/en-us/library/aa288454(v=VS.71).aspx
EDIT: for get attributes from interface look at here

Related

Accessing a property from its attribute

Is it possible to access the type of a property from an attribute that has been implemented on to that property?
public class FooAttribute : Attribute
{
public string GetPropertyName()
{
// return ??
}
}
public class Bar
{
[FooAttribute]
public int Baz { get; set; }
}
I would like GetPropertyName() to return "Baz".
Sriram Sakthivel is correct that this is not possible but if you are using .net 4.5 you can create a workaround using the CallerMemberNameAttribute to pass the caller into the constructor of your attribute, store it and then return it from your GetPropertyName method:
public class FooAttribute : Attribute
{
public string PropertyName { get; set; }
public FooAttribute([CallerMemberName] string propertyName = null)
{
PropertyName = propertyName;
}
public string GetPropertyName()
{
return PropertyName;
}
}
This will pass the caller (the property) to the constructor of your attribute.
More details on the CallerMemberNameAttribute are available on MSDN.
What you're asking is not possible. Because Attributes and properties doesn't have "One to One" relationship. You can apply FooAttribute to any number of Properties, In such case which property you need to return from GetPropertyName method?
As I said in comments you can loop through all the types and its properties to see which are all the properties have FooAttribute but obviously that's not what you want.

Giving parameters a metatag for reflection

I would like to flag a parameter in such a way that I can read the tag via reflection. The reason I want to do this is because I am creating business layer objects that map to a database and I want to flag certain parameters as 'read-only', such as uniqueidentifier fields that are being generated in the database.
I am already iterating through the properties for filling the parameters. This is a snippet of how I'm assigning values...
foreach (var prop in this.GetType().GetProperties())
{
switch (prop.PropertyType.Name)
{
case "Int32":
int tmpInt = -1;
if (!DBNull.Value.Equals(rowFromDatabase[prop.Name]) && int.TryParse(rowFromDatabase[prop.Name].ToString(), out tmpInt))
{
prop.SetValue(sender, tmpInt);
}
break;
case "Boolean":
bool tmpBool = false;
if (!DBNull.Value.Equals(rowFromDatabase[prop.Name]) && bool.TryParse(rowFromDatabase[prop.Name].ToString(), out tmpBool))
{
prop.SetValue(sender, tmpBool);
}
break;
..............
continued...
..............
}
}
I want to be able to access some kind of metadata on a parameter that is accessible via the prop variable shown above where I can specify some kind of extra information. How can I do this?
EDIT: I'd like to set the metadata like this
[CustomTag]
public Guid ID { get; set; }
Extend the class System.Attribute then decorate your properties with your custom attributes.
For example:
public class ReadOnlyAttribute : System.Attribute
{
}
or
public class DbColumnInfoAttribute : System.Attribute
{
public string ColumnName {get; set; }
public bool Required { get; set; }
public DbColumnInfoAttribute( string name, bool req){
ColumnName = name;
Required = req;
}
}
Then use them:
public class YourClass
{
[ReadOnly]
[DbColumnInfo( "User_Name", true)]
public string UserName { get; set; }
}
To read them via Reflection:
var listOfAttrib = prop.GetCustomAttributes(typeof(MyAttribute), true);
I would recommend that ALL of your attribute classes extend a common class so you can get all custom properties without having to know the exact attribute you're looking for otherwise you'll end up having to fire multiple GetCustomAttributes

System.ComponentModel.DescriptionAttribute in portable class library

I am using the Description attribute in my enums to provide a user friendly name to an enum field. e.g.
public enum InstallationType
{
[Description("Forward of Bulk Head")]
FORWARD = 0,
[Description("Rear of Bulk Head")]
REAR = 1,
[Description("Roof Mounted")]
ROOF = 2,
}
And accessing this is easy with a nice helper method:
public static string GetDescriptionFromEnumValue(Enum value)
{
DescriptionAttribute attribute = value.GetType()
.GetField(value.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false)
.SingleOrDefault() as DescriptionAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}
I need to convert this into a portable class library but it doesn't seem to have access to the System.ComponentModel library. when I try add a reverence VS tells me that I have referenced everything already.
Thanks
Since DescriptionAttribute is not available for portable class libraries you need to use another attribute. The namespace System.ComponentModel.DataAnnotations which is available for portable class libraries provides the attribute DisplayAttribute that you can use instead.
public enum InstallationType
{
[Display(Description="Forward of Bulk Head")]
FORWARD = 0,
[Display(Description="Rear of Bulk Head")]
REAR = 1,
[Display(Description="Roof Mounted")]
ROOF = 2,
}
Your method needs to be changed to
public static string GetDescriptionFromEnumValue(Enum value)
{
DisplayAttribute attribute = value.GetType()
.GetField(value.ToString())
.GetCustomAttributes(typeof(DisplayAttribute ), false)
.SingleOrDefault() as DisplayAttribute ;
return attribute == null ? value.ToString() : attribute.Description;
}
Whether something is available to a portable class library depends a bit on exactly which frameworks you selected for the library - you get the strict intersection only. However, it could well be that this attribute simply doesn't exist in one of your targeted frameworks. In which case, one option is add your own - then you know it is available. For example:
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class EnumDescriptionAttribute :Attribute
{
private readonly string description;
public string Description { get { return description; } }
public EnumDescriptionAttribute(string description)
{
this.description = description;
}
}
enum Foo
{
[EnumDescription("abc")]
A,
[EnumDescription("def")]
B
}
Note that I intentionally haven't included the additional serialization construtors here, because those too depend on features that are not available on all frameworks. Changing your code from using [Description] / DescriptionAttribute to [EnumDescription] / EnumDescriptionAttribute should be fairly trivial.
Try this for retrieving attribute for enum in portable libraries:
public static class EnumsHelper
{
public static T GetAttributeOfType<T>(this Enum enumVal) where T : Attribute
{
var typeInfo = enumVal.GetType().GetTypeInfo();
var v = typeInfo.DeclaredMembers.First(x => x.Name == enumVal.ToString());
return v.GetCustomAttribute<T>();
}
}
Update: also you should declare new attribute (look like DescriptionAttribute not available in PCL), for example next:
public class MyDescriptionAttribute : Attribute
{
public virtual string Text { get; set; }
}
and add one more method in EnumsHelper class:
public static class EnumsHelper
{
...
public static string GetDescription(this Enum enumVal)
{
var attr = GetAttributeOfType<MyDescriptionAttribute>(enumVal);
return attr != null ? attr.Text : string.Empty;
}
}
and if you have next enum:
public enum InstallationType
{
[MyDescription(Text = "Forward of Bulk Head")]
FORWARD = 0
}
you can retrieve description with code like this:
static void Main(string[] args)
{
var it = InstallationType.FORWARD;
var description = it.GetDescription();
Console.WriteLine(description);
}

Extension Method for Custom Attributes

I am working an ASP.net MVC4 website and have model & view model layer. Because of certain reasons I have different names for few properties in Model and ViewModel
Model
public partial class Project
{
public string Desc {get; set;}
}
View Model
public class ProjectViewModel
{
public string Description { get; set; }
}
Now at model layer, I need to use ViewModel name of a property if it is different. I was thinking of creating a custom attribute so that I can have something like this in models:
public partial class Project
{
[ViewModelPropertyName("Description")]
public string Desc {get;set;}
}
and use it at model layer as
string.Format("ViewModel Property Name is {0}", this.Desc.ViewModelPropertyName())
I want this to generic so that if there is no ViewModelPropertyName attribute on a property then it should return the same property name i.e. if Desc property has no attribute then it should return "Desc" only.
Here is what I tried
public class ViewModelPropertyNameAttribute : System.Attribute
{
#region Fields
string viewModelPropertyName;
#endregion
#region Properties
public string GetViewModelPropertyName()
{
return viewModelPropertyName;
}
#endregion
#region Constructor
public ViewModelPropertyNameAttribute(string propertyName)
{
this.viewModelPropertyName = propertyName;
}
#endregion
}
Need help for how to access custom attribute
Current state
public static class ModelExtensionMethods
{
public static string ViewModelPropertyName(this Object obj)
{
// ERROR: Cannot convert from 'object' to 'System.Reflect.Assembly'
System.Attribute[] attrs = System.Attribute.GetCustomAttributes(obj);
foreach (System.Attribute attr in attrs)
{
if (attr is ViewModelPropertyNameAttribute)
{
return ((ViewModelPropertyNameAttribute)attr).GetViewModelPropertyName();
}
}
return string.Empty;
}
}
But this has compile time error:
Unfortunately you can not get the attributes you used to decorate the properties by reflecting on the type of the property itself. I have therefore modified your ViewModelPropertyName(this object) Extension method slightly to take in the name of your desired property.
This method will now take in the name of the property whose attribute you wish to get. If the attribute exists it will return the value passed to its constructor, if it on the other hand, does not exist it will simply return the name of the property you passed in.
public static class ModelExtensionMethods
{
public static string ViewModelPropertyName(this object obj, string name)
{
var attributes = obj.GetType()
.GetCustomAttributes(true)
.OfType<MetadataTypeAttribute>()
.First()
.MetadataClassType
.GetProperty(name)
.GetCustomAttributes(true);
if (attributes.OfType<ViewModelPropertyNameAttribute>().Any())
{
return attributes.OfType<ViewModelPropertyNameAttribute>()
.First()
.GetViewModelPropertyName();
}
else
{
return name;
}
}
}
You can also define the following classes to test this new approach.
[MetadataType(typeof(TestClassMeta))]
class TestClass { }
class TestClassMeta
{
[ViewModelPropertyName("TheName")]
public string FirstName { get; set; }
public string LastName { get; set; }
}
Also, as you can see from the following lines of code, your ViewModelPropertyName(this object, string) Extension method will now be called on the instance of your TestClass, instead of calling it on the property itself.
class Program
{
static void Main()
{
Console.WriteLine(new TestClass().ViewModelPropertyName("FirstName"));
Console.WriteLine(new TestClass().ViewModelPropertyName("LastName"));
Console.Read();
}
}

How check whether class members are not null or empty

I have a class with only string members like this :
public class MyClass
{
public string MyProp1 { get; set; }
public string MyProp2 { get; set; }
}
I create an instance :
Var myClass = new MyClass();
Later in the code, I’d like to know if all the member (MyProp1 and MyProp2) are not null or empty. I know I can use a if of course but there is much more properties than 2 in my real code.
Is there a way to do this ?
Thanks,
Using a dictionary based store for your properties is probably the easiest way of doing this:
public class MyClass
{
private IDictionary<String, String> _store;
public MyClass()
{
_store = new Dictionary<String, String>();
}
public string MyProp1 {
get { return GetOrDefault("MyProp1"); }
set { _store["MyProp1"] = value; }
}
public string MyProp2 {
get { return GetOrDefault("MyProp2"); }
set { _store["MyProp2"] = value; }
}
public Boolean HasData()
{
return _store.Any(x => !String.IsNullOrWhiteSpace(x.Value));
}
public Boolean IsEmpty()
{
return _store.All(x => String.IsNullOrWhiteSpace(x.Value));
}
private String GetOrDefault(String propertyName)
{
if (_store.ContainsKey(propertyName))
{
return _store[propertyName];
}
return String.Empty;
}
}
Another method for doing this would be to compare it with a default instance:
public class MyClass
{
public string MyProp1 { get; set; }
public string MyProp2 { get; set; }
public static readonly MyClass Empty = new MyClass();
public Boolean HasData()
{
return !Empty.Equals(this);
}
public Boolean IsEmpty()
{
return Empty.Equals(this);
}
}
You can try to use the reflect to check the properties. You should need confirm that all the properties are public, and the type is string. Here is the code.
public static bool IsNullOrEmpty(MyClass prop)
{
bool result = true;
PropertyInfo[] ps = prop.GetType().GetProperties();
foreach (PropertyInfo pi in ps)
{
string value = pi.GetValue(prop, null).ToString();
if (string.IsNullOrEmpty(value))
{
result = false;
break;
}
}
return result;
}
To check if your class contains 'any' properties which are null:
System.Reflection.PropertyInfo[] properties = myClass.GetType().GetProperties
(BindingFlags.Public | BindingFlags.Instance);
bool hasNullProperty = properties.Any(y => y.GetValue(x, null) == null);
You can always initialize your class like
public class MyClass
{
public MyClass() {
this.MyProp1 = this.MyProp2 = String.Empty;
}
public string MyProp1 { get; set; }
public string MyProp2 { get; set; }
}
and, unless your programmatically assign a null value to it, the new MyClass() will always have String.Empty in their 2 properties...
from comment:
What I do in those cases is call a helper, for example: string name = myHelper.CheckNode(xmlNode); and in that helper I check if it's null, any other check, I can easily tweek the helper method and it will be available to all elements, and you can extend it to support not only strings but all other data types as well
So, imagine that you are reading nodes from your XML, you write them like:
string name = myHelper.CheckNode(node);
in your helper, you could have something like:
XmlNodeList datasourceNodes = rootNode.SelectNodes("dataSources/dataSource");
foreach (XmlNode datasourceNode in datasourceNodes)
{
DataSource dataSource = new DataSource();
dataSource.Name = myHelper.CheckAttr(datasourceNode.Attributes["name"]);
dataSource.ODBC = myHelper.CheckNode(datasourceNode.SelectSingleNode("odbc"));
// or a variant (Extension Method)
dataSource.UID = datasourceNode.CheckNode("user");
dataSource.PWD = datasourceNode.CheckAttr("password");
ds.Add(dataSource);
}
your helper then could have a method like:
public static string CheckAttr(XmlAttribute attr)
{
return attr == null ? "" : attr.Value.Trim();
}
public static string CheckNode(XmlNode node)
{
return node == null ? "" : node.InnerText.Trim();
}
or for the variant (Extension Method)
public static string CheckAttr(this XmlNode, string attrName)
{
return attrName[attrName] == null ? "" : attrName[attrName].Value.Trim();
}
public static string CheckNode(this XmlNode, string nodeName)
{
return node.SelectSingleNode(nodeName) == null ?
"" :
node.SelectSingleNode(nodeName).InnerText.Trim();
}
If there are many properties in the class, one way of handling this is storing them in a collection, such as an array or a dictionary, instead of declaring each property as a separate member of the class.
Then you can access data in the dictionary by key, which is as easy as accessing a property of a class. And the advantage is that you can loop over the dictionary and check all the properties in a loop.
I would suggest creating a function in your class where you check String.IsNullOrEmpty(MyProp1) etc. for all your properties. This way you at least have gathered all the ckecking functionality in a single place. And you only have this place to modify whenever you add new properties.
To check if all the elements are set you could add a IsEmpty() method to your class that would check the internal properties. Then you wouldn't have to duplicate the if statements everywhere trough your code.
In your IsEmpty() method you can use a regular if statement to check all the fields or you can use reflection to automaticaly retrieve all string properties and check their values. The performance of reflection will be worse then a normal if check but if that's not a problem you can reuse the reflection code in all your entities to check their values.
By using Attribute Base programming you can achieve this. In this approach you will need to place attribute over the class member, and validation is can be done. You can also use Microsoft Enterprise Library for this.
Probably the best way would be to :
Restructure your properties in the form of a Dictionary of strings. Loop through the dictionary to test the strings using string.IsNullOrEmpty(). You could replace the N getter/setters by a single Indexer property which sets and retrieves the strings directly from the dictionary based on a key
You can use try the following style. I haven't tried it before but you might see if it helps
If (String.IsNullOrEmpty(string1 && string2 && string3))

Categories