New Posts New Posts RSS Feed: Retreive max string length at runtime
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Forum LockedRetreive max string length at runtime

 Post Reply Post Reply
Author
chuckc View Drop Down
Groupie
Groupie


Joined: 27-Feb-2010
Posts: 54
Post Options Post Options   Quote chuckc Quote  Post ReplyReply Direct Link To This Post Topic: Retreive max string length at runtime
    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!
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
 
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, where someone had a similar question.
 
I hope it helps,
   Silvio.
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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
 
Silvio.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down