In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
C# reflection how to use the process and steps, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Example:
1. According to the name of the window class, a new window is generated, which is equivalent to the new window class
/ / 1. Define the window class name: (the string name of the window class, the full path name is required, otherwise you can't get the TYPE) string customClassName = @ "IBAutoDeal.IBDealForms." + hasPanel.Text; / / 2. Gets the string customClassName (the string name of a window class) the TypeType customClassType = Type.GetType (customClassName) of the specified class; / / 3. Generate an instance of the specified Type, which is equivalent to new class object customClassObj = Activator.CreateInstance (customClassType); / / 4. To operate on the newly generated class, this example assigns the window from new to a window variable panelForm = customClassObj as Form; / / thus generating an instance of the class using the string name of the class for use by subsequent programs
two。 Assign a value to a field or property of a class based on the string name of the class
/ / reflection function use and steps: the following example generates an instance of a string class name and assigns a value / / 1 to the properties or fields of the class. Define the class name: (the string name of the class, the full path name is required, otherwise you won't get TYPE) string customVaribleName = @ "IBAutoDeal.IBDealClass.CommVar"; / / 2. Get the string customVaribleName TypeType customVaribleType = Type.GetType (customVaribleName) of the specified class; / / 3. Generate an instance of the specified Type, which is equivalent to new class object customAaribleObj = Activator.CreateInstance (customVaribleType); / / 4. To get a field of this class, this example assigns a value to a public form variable, which here is a field / / if it is to assign a value to a property of this class, you need to use PropertyInfo pi = customVaribleType.GetField ("v" + hasPanel.Text) / / in this class, is a field a property or a field? My personal judgment is that if the field provides a GET,SET method, it is an attribute, otherwise it is a field, and I don't know if it is correct. FieldInfo pi = customVaribleType.GetField ("v" + hasPanel.Text); / / 5. Assign a value to the field. The field is a window class variable. CustomClassObj is the window class pi.SetValue (customAaribleObj, customClassObj) generated from the string in example 1. / through the above five steps, the field of the corresponding class is assigned according to the name of the string.
3. According to the string name of the class, read the field value and use it (in the example, the field value is the form, and the example is to destroy the form)
/ / 1. Define the class name: (the string name of the class, the full path name is required, otherwise you won't get TYPE) string customVaribleName = @ "IBAutoDeal.IBDealClass.CommVar"; / / 2. Get the string customVaribleName TypeType customVaribleType = Type.GetType (customVaribleName) of the specified class; / / 3. Generate an instance of the specified Type, which is equivalent to new class object customAaribleObj = Activator.CreateInstance (customVaribleType); / / 4. To get a field of this class, this example assigns a value to a public form variable, which here is a field / / if it is to assign a value to a property of this class, you need to use PropertyInfo pi = customVaribleType.GetField ("v" + hasPanel.Text) / / in this class, is a field a property or a field? My personal judgment is that if the field provides a GET,SET method, it is an attribute, otherwise it is a field, and I don't know if it is correct. FieldInfo pi = customVaribleType.GetField ("v" + e.Panel.Name); / / 5. Read the value of the field (in this example, the field value is the form, read the form variable, destroy the form) (pi.GetValue (customAaribleObj) as Form). Dispose (); / / 6. Then assign a null value pi.SetValue (customAaribleObj, null) to the field
4. Example 3 originally uses switch to determine which window needs to be closed, so there will be a lot of case statements. Through reflection, it is replaced by six lines of code in example 3. The code of the original program is posted below to let you understand the effect of reflection:
/ / the original code snippet replaced by example 3 (closes the corresponding window according to the value of e.Panel.Name and assigns values to common variables) switch (e.Panel.Name) {case "FrmAccountStatistics": CommVar.vFrmAccountStatistics.Dispose (); CommVar.vFrmAccountStatistics = null; break Case "frmPositionManager": CommVar.vfrmPositionManager.Dispose (); CommVar.vfrmPositionManager = null; break; case "frmTrader": CommVar.vfrmTrader.Dispose (); CommVar.vfrmTrader = null; break Case "frmLog": CommVar.vfrmLog.Dispose (); CommVar.vfrmLog = null; break; case "frmDataTransPlant": CommVar.vfrmDataTransPlate.Dispose (); CommVar.vfrmDataTransPlate = null; break Case "frmAccountTJ": CommVar.vfrmAccountTJ.Dispose (); CommVar.vfrmAccountTJ = null; break; case "frmGridViewConfig": CommVar.vfrmGridViewConfig.Dispose (); CommVar.vfrmGridViewConfig = null; break Case "frmTaticlistExcel": CommVar.vfrmTaticlistExcel.Dispose (); CommVar.vfrmTaticlistExcel = null; break; case "frmQuoteLst": CommVar.vfrmQuoteLst.Dispose (); CommVar.vfrmQuoteLst = null; break Case "frmProduct": CommVar.vfrmProduct.Dispose (); CommVar.vfrmProduct = null; break; default: break;}
5. For a class defined by T's pantype, the sample code for assigning an attribute is as follows:
Public static T ReadClass (string pClassName) where T:class {/ / 1. Define the class name: (because there is a T class passed in, the class name is not needed) / / 2. To get the TYPE of class T, note that in the previous example, the type is fetched through a string, using Type.GetType () Type classType = typeof (T); / / 3. Generate an instance of the specified T. In the previous example, we used Activator.CreateInstance (classType) T ClassObj = Activator.CreateInstance (); / / 4. Define the property variables of the class. Because the program has to read different properties several times, define a local variable PropertyInfo classPropertyInfo = null; string tableName = "TradeParameters"; / / DataTable must be converted to DataView to use RowFilter DataTable vDt = DataTableHelper.ReadTable (tableName); DataView vDv = vDt.DefaultView VDv.RowFilter = $"ClassName ='{pClassName}'"; if (vDv.Count > 0) {string pName, pVaule; foreach (DataRow dr in vDv) {pName = dr ["AttributeName"] .ToString (); pVaule = dr ["AttributeValue"] .ToString () / / 5. Get the specified name attribute classPropertyInfo = classType.GetProperty (pName); if (classPropertyInfo! = null) {/ / 6. Assigning a value of classPropertyInfo.SetValue (ClassObj, pVaule) to the specified property;} return ClassObj;} is it helpful for you to read the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.