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.
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...
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);
}
}
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).
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?
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.