How can I browse items of ICollection and check their types

时间:2022-06-07 22:57:06

I wrote a validation attribute for entire images. I have two properties (HttpPostedFileBase, ICollection <HttpPostedFileBase>) . I can check the first but not the second.

我为整个图像写了一个验证属性。我有两个属性(HttpPostedFileBase,ICollection )。我可以检查第一个但不是第二个。

[MyFileType( " jpg , jpeg , png " )]
public HttpPostedFileBase file128 { get; set; }
public ICollection<HttpPostedFileBase> file900 { get; set; }  

public class MyFileTypeAttribute : ValidationAttribute
{
    private readonly List <string> myTypes;
    public MyFileTypeAttribute(string _types)
    {
         MyTypes =_types. Split ( ' ,  ' ).tolist ();
    }
    public override boo Isvalid (object _value)
    {
         if (_value == null )
         {
            return true ; 
         }

         var fileExt = System.IO.Path.GetExtention ((_value as HttpPostedFileBase ).FileName ).Substring (1);
         return MyTypes. Contains ( fileExt , StringComparer.OrdinalIgnoreCase);
    }

    public override string FormatErrorMessage (string _name)
    {
        return String.Format (" invalid file type .only following types : {0} are supported . " , String.join ("," , MyTypes));
    }
}

I can check HttpPostedFileBase but how can I browse items of ICollection <HttpPostedFileBase> and check their types?

我可以检查HttpPostedFileBase但是如何浏览ICollection 的项目并检查它们的类型?

1 个解决方案

#1


0  

In case of code:

如果是代码:

[MyFileType( " jpg , jpeg , png " )]
public ICollection<HttpPostedFileBase> file900 { get ; set; }  

attribute regards the collection itself, but you can check within IsValid if attribute is specified for collection value. Something like (not tested) code:

属性关注集合本身,但您可以在IsValid中检查是否为集合值指定了属性。类似(未经测试)的代码:

public override bool Isvalid (object _value)
{
     var col = _value as ICollection<HttpPostedFileBase>;
     if (col != null )
     {
        // check each element; 
        return col.All(x => IsValid(x));
     }
     else
     {
         var fileExt = System.IO.Path.GetExtention((_value as HttpPostedFileBase ).FileName).Substring(1);
         return MyTypes.Contains (fileExt, StringComparer.OrdinalIgnoreCase);
     }
}

#1


0  

In case of code:

如果是代码:

[MyFileType( " jpg , jpeg , png " )]
public ICollection<HttpPostedFileBase> file900 { get ; set; }  

attribute regards the collection itself, but you can check within IsValid if attribute is specified for collection value. Something like (not tested) code:

属性关注集合本身,但您可以在IsValid中检查是否为集合值指定了属性。类似(未经测试)的代码:

public override bool Isvalid (object _value)
{
     var col = _value as ICollection<HttpPostedFileBase>;
     if (col != null )
     {
        // check each element; 
        return col.All(x => IsValid(x));
     }
     else
     {
         var fileExt = System.IO.Path.GetExtention((_value as HttpPostedFileBase ).FileName).Substring(1);
         return MyTypes.Contains (fileExt, StringComparer.OrdinalIgnoreCase);
     }
}