Converts a class (or Object) into a dictionary and an object class.
Directly run the code: convert a class to an object and convert it to a dictionary.
1 internal static IDictionary<string, string> GetDictionary(this object source) 2 { 3 if (source == null) 4 { 5 return new Dictionary<string, string>(); 6 } 7 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(source); 8 Dictionary<string, string> dictionary = new Dictionary<string, string>(); 9 for (int i = 0; i < properties.Count; i++)10 {11 dictionary.Add(properties[i].Name, properties[i].GetValue(source).ToString());12 }13 return dictionary;14 }
HtmlAttributes is an objuect type (an anonymous class new {name = "James "})
Call: IDictionary <string, string> dictionary = this.html Attributes. GetDictionary ();
How to convert an Object type to List type
First, the page header <% @ page import = "full package name of the Question class" %> <% @ page import = "java. util. list "%> and then list <entity> userlist = (list <entity>) session. getAttribute ("userList ")
How to convert an object to a numeric type in java
Hello,
You can add a judgment first.
If (obj instanceof Integer ){
Integer temp = (Integer) obj;
}
Or directly convert
If (null! = Obj ){
Integer temp = Integer. valueOf (obj. toString ());
}