Tim Field Posted October 20, 2005 Posted October 20, 2005 Stuck on a bit of reflection.... Hi all, I'm not 100% on what to do here so hopefully someone can help out. I have a class that has various objects on it. These are one of the following... stringAttribute doubleAttribute integerAttribute and each on holds a field from the database (obviously this is loaded into C#). On these there is a property called ReadOnly. Where it is one of these properties I'd like to get ReadOnly to whatever is passed in. I'm not sure how to do this a rough guess is below. Am I along the right lines??? Thanks! Tim public void SetReadOnlyForAllAttributes(bool readonly) { Type MyType = Type.GetType(testclass); MemberInfo[] MyMemberInfoArray = MyType.GetMembers(); for ( int counter = 0; counter < MyMemberInfoArray.GetLength(0); counter++ ) { if (MyMemberInfoArray[counter].MemberType.ToString() == "stringAttribute") { MyMemberInfoArray[counter].ReadOnly = readonly; } } } Quote
Administrators PlausiblyDamp Posted October 20, 2005 Administrators Posted October 20, 2005 Could you give a bit more detail about what you are trying to do with this? Does the testclass code have attributes applied to it? If so they are compiled into the assembly and are not changable at runtime anyway - so I'm not too sure what the .ReadOnly thing is attempting to do. If possibly could you post an example of the testclass as well as what you are expecting the above code to do. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Diesel Posted October 20, 2005 Posted October 20, 2005 Postfixing Attribute to a class is not a good idea unless it inherits from System.Attribute. Do you have access to the classes you want to set readonly? If so, then reflection is not needed. Also if you know the type of class, reflection is not needed. Are the fields in these classes static? using System; namespace ReflectionTest { /// <summary> /// Summary description for SuperString. /// </summary> public class SuperString { private string text; private bool readOnly; public string Text { get { return this.text; } set { if (!(this.readOnly)) { this.text = value; } } } public bool ReadOnly { get { return this.readOnly; } set { this.readOnly = true; } } public SuperString(string text) { this.text = text; } } } Quote
Tim Field Posted October 21, 2005 Author Posted October 21, 2005 Hi Guys, Thanks for trying to suss this out. Ok here's why and what I'm trying to do. I did try to make this example simpler! I have an abstract class that will have this reflection functionality on it. From this I have around 20 classes that inherit. Each of these will have various objects on them called IntergerAttribute, DoubleAttribute, StringAttribute etc. These "attributes" are actually objects that store the data and can do a fair bit of validation, e.g. DoubleAttribute can have a max and a min. It can also be set to read only. So as I add more and more of these classes based on my abstract class I'd like reflection to pick up the "read only" functionality. Is that clear? Thanks, Tim Quote
Administrators PlausiblyDamp Posted October 21, 2005 Administrators Posted October 21, 2005 Are these 'attributes' .Net Attributes or just classes you have termed attributes? If the former then this doesn't sound like typical attribute useage. If the latter then as Diesel mentioned earlier your naming convention could be causing some confusion here - I'm not entirely sure Reflection will work with your own custom classes in this way... If possible could you post the code (or a cut down version) of something like an IntegerAttribute and how this would be used / applied to one of your other classes. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.