0

I am busy building an Enum class to validate a file being imported. I created a custom Attribute class that will take in specified criteria. The problem I am facing is a data type conversion must happen with the data type specified in die Attribute. I want to make it as generic as possible.

This is what I have:

ENUM

[Description("Company ID")]
[ColumnValidation(typeof(int), true, -1)]
CompanyID,

[Description("Company Name")]
[ColumnValidation(typeof(string), true, 50)]
CompanyName,

[Description("Company Status")]
[ColumnValidation(typeof(string), true, 50)]
CompanyStatus,

I have extension methods that will get each attribute value, the one I am having problems with is:

public static T ToDataType<T>(this WoWFileStructure val, object valueToConvert)
{
     try
     {
         return (T)Convert.ChangeType(valueToConvert, val.GetDataType());
     }
     // Catch format exception when unabe to cast value to required datatype and cast object to its default value
     catch (FormatException)
     {
         var instance = Activator.CreateInstance(typeof(T));
         return (T)instance;
     }
}

This method gets the Type:

public static Type GetDataType(this WoWFileStructure val)
{
    return val.GetAttribute<ColumnValidationAttribute>().GetPropertyType();
}

This is how I call it:

var type = enum.CompanyID.GetDataType();
var value = enum.CompanyID.ToDataType<type>("test");

But then I get error saying:

'type' cannot be used like a type

EDIT Attribute Class

[AttributeUsage(AttributeTargets.Field)]
public class ColumnValidationAttribute : Attribute, IExtendedEnumAttribute
{
    private Type propertyType;
    private bool isRequired;
    private int stringLength;

    public ColumnValidationAttribute(Type propertyType, bool isRequired, int stringLength)
    {
        this.propertyType = propertyType;
        this.isRequired = isRequired;
        this.stringLength = (int)stringLength;
    }

    public Type GetPropertyType()
    {
        return propertyType;
    }

    public bool IsRequired()
    {
        return isRequired;
    }

    public int GetStringLength()
    {
        return stringLength;
    }
}
Gericke
  • 2,109
  • 9
  • 42
  • 71
  • you could do : `var value = enum.CompanyID.ToDataType("test");` – Ehsan Sajjad Mar 06 '18 at 12:16
  • Your `GetDataType` does not do what you think it does. It will return `typeof(ColumnValidationAttribute)` – Camilo Terevinto Mar 06 '18 at 12:18
  • What type do you want/expect `value` to be of? The error you see lies in the fact that all generic type parameters must be known at compile-time. – Tewr Mar 06 '18 at 12:22
  • I made an EDIT. GetDataType was a mistake – Gericke Mar 06 '18 at 12:22
  • @Tewr what I expect to do is to convert the value to the specified DataType specified on the enum – Gericke Mar 06 '18 at 12:24
  • 1
    This has nothing to do with attributes, you want to call a generic method with a type only known at runtime. – CodeCaster Mar 06 '18 at 12:27
  • 1
    @Gericke: I see why you would say that, but that was not my question. Unless you use reflection heavily, T must be known at compile-time. You are currently "calculating" T, which is not possible. So what do you want to DO with value afterwards? as it is, it may only be of type `object`. Give some more context on what you want to do with `value`, I suspect this is currently an [xy question](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Tewr Mar 06 '18 at 12:29
  • @Tewr O I want to validate that the values can be converted to specified data type. This is to validate a file and the records before storing it – Gericke Mar 06 '18 at 12:53
  • Well I can't answer your question anymore, but I'd suggest dump your enum and your attributes altogether and use a class with strongly typed members instead: `class ReplacesEnum { static Validator CompanyId = new Validator(true, -1); }` – Tewr Mar 06 '18 at 13:04

0 Answers0