CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2007
    Posts
    59

    Is it possible to get PropertyInfo from the property declaration?

    Hello,

    I was wondering is it was possible to get the PropertyInfo directly from the code? What I mean is that I don't want to have a string base method call. To be clearer, I will show an example of What I would like to have in theory:

    Code:
    class Foo
    {
        public int X { get { return 0; } set {} }
    }
    
    // static method somewhere
    public static string Stringize(PropertyInfo pi)
    {
        // code here
        return myStr;
    }
    
    // the code I would like to write.
    string s = Stringize(Foo.X);
    The goal is to have a compile time error is the property X is modified or removed. The syntax is not really a problem, but this example should show clearly what I'm looking for.

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    Re: Is it possible to get PropertyInfo from the property declaration?

    Quote Originally Posted by angedelamort
    The goal is to have a compile time error is the property X is modified or removed. The syntax is not really a problem, but this example should show clearly what I'm looking for.
    I'm not sure i understand... You want a compile-time error if you decide to remove a property from a class? You also want a compile-time error if someone decides to change the properties return type?

    If so, just use the property somewhere in your code and you'll be fine...
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Is it possible to get PropertyInfo from the property declaration?

    Quote Originally Posted by Mutant_Fruit
    If so, just use the property somewhere in your code and you'll be fine...
    That is very true. It looks like you want early binding, so why not to use the property directly? But if you really want what you've written, you could do it outside the C# with some static code analysis. E.g. you can create your own rule for FxCop.

    Also, you can achieve similar goal with delegates. Tak following as inspiration:
    Code:
      delegate T StringizeCandidate<T>();
    
      class Foo
      {
        public int X { get { return 0; } set { } }
    
        public int GetX()
        {
          return this.X;
        }
    
        // static method somewhere
        public static string Stringize<T>(StringizeCandidate<T> method)
        {
          string myStr = method().ToString();
          // code here
          return myStr;
        }
    
        public static void FooBar()
        {
          Foo f = new Foo();
          // the code I would like to write.
          string s = Stringize<int>(f.GetX);
        }
      }
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4
    Join Date
    Mar 2007
    Posts
    59

    Re: Is it possible to get PropertyInfo from the property declaration?

    My goal is really simple, I want to be able to build a string at "compile time".

    example:

    Code:
    string str = "SELECT * FROM " + 
        Stringize(typeof(Foo)) + " WHERE " +
        Stringize(propertyof(Foo.X)) + "='1'";
    And boudino, thanks for your answer but I've already tried that. What I don't like is that you have to define a Method (since delegate doesn't works with Properties).

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: Is it possible to get PropertyInfo from the property declaration?

    Quote Originally Posted by angedelamort
    My goal is really simple, I want to be able to build a string at "compile time".

    example:

    Code:
    string str = "SELECT * FROM " + 
        Stringize(typeof(Foo)) + " WHERE " +
        Stringize(propertyof(Foo.X)) + "='1'";
    You can't. That code won't be executed until run-time. There's no way for the compiler you execute your code at compile time. The only things that *may* be condensed like you want are constructs like:

    string s = "a" + "b" + "c";

    where all the literals are known at compile time. But in that case you'd have just written this anyway:

    string s = "abc";

    EDIT: Why do you want this anyway? You're really tightly coupling your database to your class implementation. What's the benefit?
    Last edited by Mutant_Fruit; March 17th, 2008 at 06:45 PM.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Mar 2007
    Posts
    59

    Re: Is it possible to get PropertyInfo from the property declaration?

    I'm trying to abstract the name of the property. The reason is that if a certain attribute is defined for this property, I want to use it. Also, I don't want people to use "strings" when creating a select statement for example.

    In my example, you can see that I'm able to get the typeof the class to generate the string. It's trivial. I was wondering if there was a way to do that for the property. Right now, my function is defined like that:

    Code:
    public static string Stringize(Type classType, String propertyName);
    It seems that it's the only way possible. So I'm looking for a version that doesn't take a string if it's possible.

    And to answer your question, I'm making a DbSerializer in the same way of the XmlSerializer. So, that's why I want a tightly coupling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured