Reflection

Using reflection you can discover which types exist in an assembly at run-time as well as examine its methods, properties, events and attributes.


what is attribute, what can you use it for ?
what are the pros and cons



Attributes are metadata tags that you can apply to your code.
The CLR uses classes within the .NET Framework that are part of the System.Reflection namespace to programmatically inspect an assembly.


The ildasm utility uses reflection to display all the types and members of an assembly.
You can also view the assembly's IL.
There are other tools that use reflection on an assembly that do not ship with the Framework, such as .NET Reflector.


System.Reflection


System.Activator


CreateInstance


System.Reflection.Emit


                string asmPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + 
                                        "\\Better Solutions\MyCode.dll";

                System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(asmPath);
                if (assembly != null)
                {
                    System.Type myData_Type = assembly.GetType("MyCode.XML+AnotherObject");
                    var myData_Instance = Activator.CreateInstance(myData_Type);

                    System.Reflection.FieldInfo fieldInfo_City = coverData_Type.GetField("City");
                    fieldInfo_City.SetValue(myData_Instance, "London");

                    System.Collections.Generic.List<MyListOfSomething> myList;
                    if (excelApp == null)
                    {
                        myList = getTheList();
                    }
                    else
                    {
                        myList = getTheList(excelApp.ActiveWorkbook);
                    }

                    System.Type myData_Type2 = assembly.GetType("MyCode.XML+AnotherObject");

                    var listType = typeof(List<>).MakeGenericType(new[] { myData_Type2 } );
                    var AnotherList = (System.Collections.IList)Activator.CreateInstance(listType);

                    foreach (CommonData.Analyst analyst in analysts)
                    {
                        var one_Instance = Activator.CreateInstance(myData_Type2);

                        System.Reflection.FieldInfo fieldInfo_DisplayName = myData_Type2.GetField("DisplayName");
                        fieldInfo_DisplayName.SetValue(one_Instance, analyst.displayName);

                        AnalystsList.Add(one_Instance);
                    }

                    System.Reflection.FieldInfo fieldInfo_CoverDataAnalysts = coverData_Type.GetField("Analysts");
                    fieldInfo_CoverDataAnalysts.SetValue(coverData_Instance, AnalystsList);

                    if (dataPageTable != null)
                    {
                        System.DateTime startTime = System.DateTime.Now;

                        System.Type client_Type = assembly.GetType("myCode.Client");
                        var client_Instance = Activator.CreateInstance(client_Type);

                        System.Type myapplication_Type = assembly.GetType("myCode.Application");
                        System.Reflection.PropertyInfo property_myapp = client_Type.GetProperty("MyApplication");
                        var myapp_Instance = System.Convert.ChangeType(property_myapp.GetValue(client_Instance, null), myapplication_Type);

                        System.Type wordxml_Type = assembly.GetType("myCode.WordXML");
                        System.Reflection.PropertyInfo property_excelinformation = myaapplication_Type.GetProperty("ExcelInformation");
                        var wordxml_Instance = System.Convert.ChangeType(property_excelinformation.GetValue(myapp_Instance, null), wordxml_Type);

                        object[] arguments = new object[] { dataPageTable, false };

                        object returned = wordxml_Type.InvokeMember("CallThisMethod",
                            System.Reflection.BindingFlags.InvokeMethod, null, wordxml_Instance,
                            arguments);

                        dataPageFilePath = (string)returned;

                        Excel.Range cells = null;
                        ((Excel._Worksheet)this._worksheet).Activate();
                        cells = this._worksheet.get_Range("A1");
                        cells.Select();
                        embedDataPageObject = this._worksheet.Shapes.AddOLEObject(System.Type.Missing, dataPageFilePath, false, false);
                        CoverSheet_DataPage.Static_CloseWord(dataPageFilePath);
                    }

                }



© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext