Advice on using StackFrame and reflection in BLL to return property values

Mark Johnson <[email protected]>
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <[email protected]>
Just for fun I was playing around with creating an interface to a
permissions object and ended up with this.  Just wondering if C# community
thought this is a good solution or not.  What I ended up doing was instead
of writing a lot of non-generic redundant code peppered with literals to
access some permission settings I used the stack frame to figure out which
defined property was getting called by the client and than querying the
cached dataset for the results.  I fully understand that just because I can
doesn't mean I should but I was impressed by what I could do with C# to cut
down on what could have been a lot of messy and redundant code.  What do you
think?

thanks for your time!

public class Permissions
{
  protected Nimbus.PermissionsDataTable thePerms = null;
  public Permissions( string aUsername )
  {
    thePerms = (new PermissionsTableAdapter()).GetData(aUsername);
  }
  protected bool getPerm()
  {
    System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1);
    System.Diagnostics.StackFrame sf = st.GetFrame(0);
    string perm = sf.GetMethod().Name.Replace("get_","");

    bool rv = false;
    try
    {
       DataRow[] rows = thePerms.Select(
String.Format("Permission='{0}'",perm) );
       // etc...
     }
     catch( Exception  )
     {
     }
     return rv;
  }
  public bool Admin            { get { return getPerm(); } }
  public bool CreateBudget { get { return getPerm(); } }
  public bool EditLineItem   { get { return getPerm(); } }
  // etc...
};

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.