简体   繁体   中英

C# cast derived class to base class exception via reflection

I have an app that creates classes dynamically using reflection. When deployed, I get an exception when casting a derived class to its base class. It only happens on 1 in 100 machines. All the classes are in the same assembly. Below are some code snippets and output from a logging message just prior to the casting exception. I'm at my wits end, any help greatly appreciated.

//Parent class
namespace Framework.DataModel
{
    [Serializable]
    public class DataTreeRequest : TreeNode, IDirtyListener, ISerializable
    {
        ....
    }
}

// Derived Class    
namespace Framework.DataModel
{
    [Serializable]
    public class CADElementRequest : DataTreeRequest
    {
        public CADElementRequest(String name) : base(name){}
    }
}


// Method that uses reflection to create class and then cast to its base class
namespace Framework.DataModel
{
    [Serializable]
    public class DataModelBuilder : CoreBuilder
    {
        ...

        protected DataTreeRequest CreateDataTreeRequest(String asmName, String inName, String inType, String inSourceName)
        {
            DataTreeRequest dtr = null;

            Assembly asm = Assembly.LoadFrom(asmName);
            if (asm == null)
            {
                throw new BaseException("Can't find assembly " + asmName);
            }

            Type requestType = asm.GetType(inType);
            if (requestType == null)
            {
                throw new BaseException("Can't find class of type " + inType + " in assembly " + asmName);
            }

            // Call the constructor for the tree node that takes the xml node as an argument
            Type[] constructorArgsTypes = new Type[1];
            constructorArgsTypes[0] = typeof(String);
            ConstructorInfo constructorInfo = requestType.GetConstructor(constructorArgsTypes);
            if (constructorInfo == null)
            {
                throw new BaseException("Can't find constructor for type " + inType + " that takes a String param");
            }

            Object[] constructorArgs = new Object[1];
            constructorArgs[0] = inName;
            Object newObj = constructorInfo.Invoke(constructorArgs);

            // Code fails on this line trying to cast derived class to base class on 1 in 100 machines
            dtr = newObj as DataTreeRequest;
            if (dtr == null)
            {
                throw new BaseException("Can't cast newObj to type DataTreeRequest. newObj = " + newObj + ", Type = " + newObj.GetType().ToString());
            }

            dtr.InSource = inSourceName;

            return dtr;
        }
    }
}

Logging output on failed machine:

Message = Found assembly=Framework.DataModel, Version=1.0.5885.31486, Culture=neutral, PublicKeyToken=null

Message = newObj AssemblyQualifiedName=Framework.DataModel.CADElementRequest, Framework.DataModel, Version=1.0.5885.31486, Culture=neutral, PublicKeyToken=null, BaseType==Framework.DataModel.DataTreeRequest, FullName==Framework.DataModel.CADElementRequest

BaseException: Can't cast newObj to type DataTreeRequest. newObj = Name=Substations;InType=;InName=Substations;OutName=Substations;InSource=;OutSource=;, Type = Framework.DataModel.CADElementRequest

try to replace

Assembly asm = Assembly.LoadFrom(asmName);
if (asm == null)
{
    throw new BaseException("Can't find assembly " + asmName);
}

Type requestType = asm.GetType(inType);

with

Type requestType = Type.GetType(inType)

where inType is assembly qualified name https://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname(v=vs.110).aspx

If you need load assembly that is not referenced by project consider using Assembly.Load method.

About disadvantages of using Assembly.LoadFrom read Remarks section in https://msdn.microsoft.com/EN-US/library/1009fa28(v=VS.110,d=hv.2).aspx

the red flag is it fails on specific machines (1 out of 100) - how many machines in total does the code fail on? This indicates that it might be the configuration of the machine rather than the code. It also makes it extremely difficult to replicate in order to help.

If it were me. I'd take a step back and simplify the situation. Write code that just does the failing task, even with other simpler classes, and then focus on a failing machine. Get as much information as possible and build up understanding.

It's likely to be referent pain. The situation your facing is only a symptom of the real problem. You're focused on the code but that's just a symptom. That's why it would be good to simplify.

Hope this helps, I'm new to stackoverflow and I know there are rules to follow for questions. I'd have made this a comment but I don't have the reputation for that.

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