Possible to do this in C#???
I have a class.....with different public properties ...
Class A {
public int x; // Non - compilable code..
public float y;
public decimal z;
}
Now can I expose only some of the public properties to an object based on some conditions???
Like:-
[code]
A aObj;
if(someCondition = true)
aObj. (Can I just expose 'x' property and NOT 'y' and 'z' ... ?? ... In Intellisense??)
esle
aOBj. (Can I just expose 'y' property and NOT 'x' and 'z' ... ?? ... In Intellisense??)
[\code]
Is this even possible??
Thanks!
Re: Possible to do this in C#???
If you were following ANY of the recognized texts, you would not even ask such a question....
Go get a good book, start at the beginning, read everything, type-in everything, step through everything with your debugger.
If you have questions, please come back and ask...:wave:
Re: Possible to do this in C#???
And the short answer would be "No". If they are public, they're public.
Re: Possible to do this in C#???
no you can't conditionally expose them, but you can make them invisible to intellisense (though users will still be able to access the public members, it won't show up in intellisense. It will show up in the object browser, reflector, and other class documentation).
you can also use internal which allows classes of the same assembly (you can also make internals visible to other external assemblies thorough an assembly level attribute), but anyone using a reference to your assembly will not.
you could conditionally expose members through use of multiple interfaces, by exposing the instance as an instance of a particular interface:
Code:
public interface IA {
string A { get; set; }
}
public interface IB {
string B { get; }
}
public interface IC {
string C { get; set; }
}
public class Foo : IA, IB, IC {
string a, b, c;
public string A { get { return a; } set { a = value; } }
public string B { get { return b; } }
public string C { get { return c; } set { c = value; } }
}
// ...
if(someCondition) ((IA)instanceOfFoo).A = "asdf"; // B and C are not included
else if(someOtherCondition) string fooB = ((IB)instanceOfFoo).B; // A and C are not included
else if(yetAnotherCondition) ((IC)instanceOfFoo).C = "qwerty"; // A and B are not included
Re: Possible to do this in C#???
And the wizard said unto thee:
Quote:
Originally Posted by
TheCPUWizard
If you were following ANY of the recognized texts, you would not even ask such a question....
I believe this is a direct quote from the movie Harry Potter. :D
To expand just a little on the topic of this thread. Why do you want to conditionally expose properties anyway? Is this for like some kind of plugin architecture?
Re: Possible to do this in C#???
Wow....Thanks MadHatter for the input...Yes...It does help me a lot .....at least this is a very good idea for my project......
Thanks again.....for being open-minded .... :)...and not thinking that this was a really pointless Q...