Print Page | Close Window

Retreive max string length at runtime

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2571
Printed Date: 23-Apr-2024 at 3:02am


Topic: Retreive max string length at runtime
Posted By: chuckc
Subject: Retreive max string length at runtime
Date Posted: 20-Mar-2011 at 6:01pm
Simple question:

How does one retrieve the max length of a entity string field at runtime?  I see the value provided to the StringLengthVerifier, but what is the technique to get that value for use in my own code?

  /// <summary>Gets or sets the Description. </summary>
    [Bindable(trueBindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="Description", AutoGenerateField=true)]
    [IbVal.StringLengthVerifier(MaxValue=1000, IsRequired=false, ErrorMessageResourceName="AccountTransactionDetail_Description")]
    [DataMember]
    public string Description {
      get { return PropertyMetadata.Description.GetValue(this); }
      set { PropertyMetadata.Description.SetValue(thisvalue); }
    }
Thanks!



Replies:
Posted By: sbelini
Date Posted: 21-Mar-2011 at 2:31am
Hi Chuck,
 
Unfortunatelly this is not a trivial task. You will need to retrieve this information from the EDMX. There is some information on how to do this at http://blogs.msdn.com/b/adonet/archive/2008/01/24/how-to-extract-csdl-from-edmx.aspx - http://blogs.msdn.com/b/adonet/archive/2008/01/24/how-to-extract-csdl-from-edmx.aspx .
 
The link above has been posted in a previous thread at http://www.ideablade.com/forum/forum_posts.asp?TID=2403&KW=maxlength&PID=9482&title=fixedlength-fields - http://www.ideablade.com/forum/forum_posts.asp?TID=2403&KW=maxlength&PID=9482&title=fixedlength-fields , where someone had a similar question.
 
I hope it helps,
   Silvio.


Posted By: sbelini
Date Posted: 21-Mar-2011 at 10:44am
Hello again Chuck,
 
You might actually be able to do that by using reflection:
 
public void GetMaxStringLengthSample() {
      var maxStringLength = GetMaxStringLengthAttribute(typeof(Customer),                     
         Customer.PropertyMetadata.CompanyName.Name);
      
       Assert.IsTrue(maxStringLength == 40, "This should not fail");
}

public int GetMaxStringLengthAttribute(Type entityType, String propertyName) {
      var propertyInfo = entityType.GetProperty(propertyName);
      var attributes = (StringLengthVerifierAttribute)propertyInfo
        .GetCustomAttributes(typeof(StringLengthVerifierAttribute), false).First();

      return attributes.MaxValue;
}
 
You can also find this snippet in its original thread at http://www.ideablade.com/forum/forum_posts.asp?TID=2152&title=string-field-maxlength - http://www.ideablade.com/forum/forum_posts.asp?TID=2152&title=string-field-maxlength
 
Silvio.



Print Page | Close Window