|
THX for your posting.
I knew I could make it this way and searched for an easier way. Furthermore, what do you do if the class has more than one field? Which element of the names ArrayList do you use?
<br />
FieldInfo [] fields= mytype.GetFields(BindingFlags.NonPublic|BindingFlags.Instance);<br />
foreach(FieldInfo finfo in fields)<br />
{<br />
names.Add(finfo.Name); <br />
}<br />
|
|
|
|
|
Why don't you look into XML serialization? Read the documentatio for the classes in the System.Xml.Serialization namespace. The XmlSerializer already supports this and you can always override defauls using the attributes also in that namespace. Why do what's been already done for you in the .NET FCL?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
THX for pointing me to this. I'm consistently astonished what else the .NET FCL has to offer.
But I think it doesn't fit right for what I'm trying to do as I also want to write comments and private fields to the XML file.
Anyway, I will keep the XmlSerializer in mind. Surely, I will need it sometimes.
|
|
|
|
|
To learn what the FCL has - browse through the class library documentation. You don't have to memorize everything, but at least understand what's there and were. As you develop .NET applications and libraries, you should also see a pattern that Microsoft uses to try to maintain consistency. Like for provider classes: you'll typically see a static Create or CreateInstance method.
If you want to serialize your class in such a way, then you'll have to do it yourself. But take a lesson from both XML and runtime serialization. A good solution would be to create a new attribute - say XmlCommentAttribute - that stores the comment for a field:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
public class XmlCommentAttribute : System.Attribute
{
private string comment;
public XmlCommentAttribute(string comment)
{
this.comment = comment;
}
public string Comment
{
get { return this.comment; }
}
} This would allow you to declaritivly define the comments for your fields:
[XmlRoot("myClass", Namespace="urn:myClass"]
public class MyClass
{
[XmlComment("This is parsed as a string.")]private string field1;
[XmlElement("myField2"), XmlComment("This is an int.")] private int field2;
} Noticed how I used the XML serialization attributes in places? This allows you to easily rename members or declare how things are stored, whether they should be elements or attributes, etc. If they aren't, you can have your serialization routine default to one or the other. Then just do something like this:
FieldInfo[] fields = myClass1.GetType().GetFields(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
foreach (FieldInfo field in fields)
{
if (!field.IsNotSerialized)
{
string name = field.Name;
bool useElement = true;
string comment = null;
XmlElementAttribute[] elemAttrs = (XmlElementAttribute[])
field.GetCustomAttributes(typeof(XmlElementAttribute))
if (elemAttrs != null && elemAttrs.Length > 0)
name = elemAttrs[0].ElementName;
else
{
XmlAttributeAttribute[] attrAttrs = (XmlAttributeAttribute[])
field.GetCustomAttributes(typeof(XmlAttributeAttribute));
if (attrAttrs != null && attrAttrs.Length > 0)
{
useElement = false;
name = attrAttrs[0].AttributeName;
}
}
XmlCommentAttribute[] comments = (XmlCommentAttributes[])
field.GetCustomAttributes(typeof(XmlAttributeAttributes));
if (comments != null && comments.Length > 0)
comment = comments[0].Comment;
}
} Basically, try to handle the basic XML elements so that you can easily override how fields are named. If you ever change private member names (not typically a good idea - always keep serialization in the back of your head when developing) you can use XML attributes to keep their serialized form the same - thus not breaking your new code against old serialized formats.
Finally, use FieldInfo to get the value. This is very easy to use. Do not just use ToString on the returned object (if it's not null). First, try to get the TypeConverter for the Type like so:
TypeConverter converter = TypeDescriptor.GetConverter(field.FieldType); Use that to convert to and from strings. If you just use ToString , you'll have to handle the parsing yourself. Some types provide their own parsing, but typically only the primatives and the methods often use different signatures. You can also use the ConvertTo class, but those only cover the primatives. Using a TypeConverter makes your code more robust.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
What steps do I need to follow for adding a new property to an existing class?
Thanks for your help.
Best regards,
Cristina
|
|
|
|
|
You would have to write your own class, inheriting from the class you want to extend. Then you just supply your property code and the code that will use that property.
RageInTheMachine9532
|
|
|
|
|
I'm a C# newbie. How can I insert a new line into a multi-line TextBox field? I've tried '\n' but instead of inserting a new line, it displays a strange character. Also, I want a particular method to execute when a user presses 'Enter' key after typing something in TextBox. Please help!
mughalali
|
|
|
|
|
|
if the other suggestion doesn't work, it may be something like or one of those sets. for the return, why not do a keypress event?
surgeproof
-------------------------------------------------------
ithium is the best.
'Science without religion is lame, religion without science is blind.' --Albert Einstein
'The pioneers of a warless world are the youth who refuse military service.' --Albert Einstein
|
|
|
|
|
Use Environment.NewLine, which will guarantee the placement of a newline.
|
|
|
|
|
Not necessarily in the TextBox (which encapsulates the Edit common control). To dislay new lines in a TextBox , the Multiline (inheritted from TextBoxBase ) needs to be set to true , as well as the AcceptsReturn property.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
how can i got the version infomation about the sqlsever and any similar information?
should i use the WMI? but which class should i used in the win32_InstalledApplication?
|
|
|
|
|
Run this query:
SELECT @@VERSION
Or run this SP:
xp_msver
Mazy
"One who dives deep gets the pearls,the burning desire for realization brings the goal nearer." - Babuji
|
|
|
|
|
I want to get this information in my c# web application in the run time.
is the select @@version could complie and run in the c#?!
|
|
|
|
|
yes.
it will run.. this has no relation with the programming language used.
|
|
|
|
|
Please forgive my stupidity.
i just feel puzzle,how can i use the "select @@version" in the code? would you like to show a example?
Thank you very much
|
|
|
|
|
See SqlCommand in MSDN or search for it in this site or Google to see how to run commands and SP in SqlServer from your C# application. When I test @@version and that SP the second one return informations for me , don't know wht so I recommed second way to you.
Mazy
"One who dives deep gets the pearls,the burning desire for realization brings the goal nearer." - Babuji
|
|
|
|
|
Thank you very much.
i have studyed the WMI for several hours to get the os information and the sql information.
i am not conscious that i could get the sql server information from the sql statement.
So when you told me use "select @@version", i just think how can i use it in the WMI. i am sorry for it.
Thanks a lot!
|
|
|
|
|
I have written a C# Windows form with a panel on top of it. As the program runs, a directory is opened and for each image file in the directory a picturebox is dynamically created and loaded onto the panel. As I create each picture box, I give it a Click event handler (the same function for each picture box) where I want to bring up a SaveFileDialog. This all works fine, however, I cannot figure out how to get to the individual picture box that was clicked on so that I can access the image it contains during the event. Can anyone help?
Thank you,
Dustin
|
|
|
|
|
In your event-handler you have a parameter: object sender , use this.
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
Just cast the sender to a PictureBox and it will be the one that was clicked on.
private void PictureBox_Click(object sender, System.EventArgs e)<br />
{<br />
PictureBox clickedon = (PictureBox)sender;<br />
<br />
}
|
|
|
|
|
Thanks for the replies.
That was the solution I needed.
I thought it had something to do with the sender object, I had even tried casting it as a PictureBox but kept getting compile-time errors. I never even thought of making a new PictureBox object and assigning it. Thanks again.
Dustin
|
|
|
|
|
DustinMiles wrote:
I never even thought of making a new PictureBox object and assigning it
You are not actually making a new picture box object with the previous code. What is happening is that you are creating a reference to the picture box. The variable is a reference to, not a copy of, the PictureBox.
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell
Not getting the response you want from a question asked in an online forum: How to Ask Questions the Smart Way!
|
|
|
|
|
How to set background Image of treeview in C#?
I try to code about this problem for long time , but i can't found anyway for set background of treeview in C#.
Anyboby help me , please !!!
|
|
|
|
|
If I rememebr correctly, the TreeView control doesn't support a background image, nor does it support a transparent background. You'd have to draw the control yourself to get an image to show through it or behind the actual tree that is drawn. I can't seem to find any examples of it, but you might want to look into overriding WndProc.
RageInTheMachine9532
|
|
|
|