简体   繁体   中英

Using Reflection to get BaseType Class DeclaredFields

I see lots of similar questions regarding reflection, but not exactly what I'm looking for. I am calling a dll at runtime using Assembly.LoadFile(). Once I have loaded the dll, I need to retrieve a class, then all of its Base class property values (that are preset in that dll code). I thought I was close, but I'm missing how to cast, or maybe I am completely wrong.

I'm trying to retrieve the class highlighted in the picture below 试图让班级脱离突出显示的行

            //This is really all I have thus far
            var assembly = Assembly.LoadFile(dllFile);
            var primaryInterface = assembly.DefinedTypes.FirstOrDefault(x => x.Name == "VpInterface");
            //This is wrong, not giving me expected fields
            var features = ((System.Reflection.TypeInfo)primaryInterface.BaseType.GetField("Features").GetType())
                .DeclaredFields;


//Part of the class I am trying to retrieve
public class IntegratorFeatures
{
    public string Registration { get; set; }
    public string DefaultPort { get; set; }
    public string PrimaryInterface { get; set; }
    public string LiveCaptureType { get; set; }
    public string CapturedStreamType { get; set; }
    public int PtzFeatures { get; set; }
    //etc, about 20 more properties after this

I'm not real familiar with Reflection for such advanced tasks. Once I have the above issue coded, I then have to figure out how to call methods within each of the classes in the dll. But I somehow suspect that will be easier than this initial call (hopefully that is a correct assumption)

Thanks in advance for the assistance

Try this:

public List<System.Reflection.PropertyInfo> GetFields(string AssemblyFilePath)
    {
        return System.Reflection.Assembly.LoadFile(AssemblyFilePath).GetTypes().First(x => x.Name == "The class name you want to get the fields for.").GetProperties().ToList();
    }

This will return a list of PropertyInfos which, can be used to find the name of the property.

I figured it out. This code works, returning the desired base class I was looking for:

        var assembly = Assembly.LoadFile(dllFile);
        var intPtr = new IntPtr(1);
        var primaryInterface = assembly.DefinedTypes.FirstOrDefault(x => x.Name == "VpInterface");
        var baseInt = new BaseInterface(intPtr);
        var features = 
            (IntegratorFeatures)primaryInterface.BaseType
                .GetField("Features", BindingFlags.Public | BindingFlags.Instance)
                .GetValue(baseInt);

My original understanding of how GetValue worked, was that I was getting an instance of the desired class. But it seems to need to match the instance I am getting in Reflection, which is actually the base class itself, which contains the desired class. Still a bit confusing to me, but at least it gets the properties I need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM