Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7921183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:29:27+00:00 2026-06-03T16:29:27+00:00

I’m working on a C# application that is supposed to examine other C# executables

  • 0

I’m working on a C# application that is supposed to examine other C# executables and assert, via unit tests, certain properties about the interfaces they expose. (For context, this is a grading application for a CS course; the assignment is to parse numeric ranges from a text file and return them according to a fixed interface.)

So far, I’ve managed to:

  • Load the executable as a variable assembly, using Assembly.LoadFrom(string)
  • Get the type of the interface in question, using assembly.GetType(string)
  • Find an implementing type for the interface, again with assembly.getType(string)
  • Instantiate the implementing type into a dynamic object, using type.GetConstructor(Type[]) and constructor.Invoke(Object[])

At this point, I have a dynamic object loader that I know implements the interface I’m testing. I want to call one of the interface methods on obj, so I run:

dynamic rangeSet = loader.GetRangeSetFromFile (inputFile); // inputFile is a string

This throws an InvalidCastException with the following trace:

System.InvalidCastException : Cannot cast from source type to destination type.
at SwapAssignment3.Implementations.RangeLoaderAdapter.GetRangeSetFromFile (string) <IL 0x0001e, 0x00066>
at (wrapper dynamic-method) object.CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,string) <IL 0x00036, 0x0007b>
at System.Dynamic.UpdateDelegates.UpdateAndExecute2<object, string, object> (System.Runtime.CompilerServices.CallSite,object,string) <0x003cf>
at AssignmentTests.R3Test.TestLoadingViaInterface () [0x00054] in /Users/tim/Dropbox/Courses/CSSE375-TA/AssignmentTests/AssignmentTests/R3Test.cs:82
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) <0x00003>
at System.Reflection.MonoMethod.Invoke (object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo) <IL 0x000db, 0x00147>

Why would this error be thrown? Running the executable in question works just fine on its own; this error is only thrown in the context of my testing application. The body of the GetRangeSetFromFile method is as follows:

public IRangeSet GetRangeSetFromFile(string filePath)
{
    var newRange = new RangeStorage();
    _fileProcessor.ProcessFile(filePath);
    newRange.StoreElements((List<IRange>) _fileProcessor.GetStorage());
    return newRange;
}

I have good reason to believe (from the program output, among other things) that the cast error is being raised from the third line, with the List<IRange> cast; however, since the trace gives IL locations, I’m not 100% sure of this, and I don’t know why that cast would fail in the first place, since it works fine if the program is run on its own (outside my tester).

My primary question is: why is this cast error being raised, and how can I avoid it?

Edit: by request, the test code amounts to the following:

Type interfaceType = assembly.GetType("IRangeLoader");
List<Type> implementingTypes = new List<Type> (assembly.GetTypes ())
                                        .FindAll ((Type t) => t.IsClass)
                                        .FindAll ((Type t) => (new List<Type> (t.GetInterfaces ())).Contains (interfaceType));
Type implementingType = implementingTypes[0];
ConstructorInfo ctor = implementationType.GetConstructor (new Type[] {});
dynamic loader = ctor.Invoke (new Object[] {});
dynamic rangeSet = loader.GetRangeSetFromFile ("sample range.txt");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:29:29+00:00Added an answer on June 3, 2026 at 4:29 pm

    Ok, the following code seemed to work in invoking the method in another assembly:

    Assembly testAssembly = Assembly.LoadFile(<path>);
    
    var interfaceType = testAssembly.GetTypes().Where(x => x.Name == "ISampleInterface").FirstOrDefault();
    
    if(interfaceType != null)
    {
        var implementingType = testAssembly.GetTypes().Where(typ => type.GetInterfaces().Any(iface => iface == interfaceType)).FirstOrDefault();
    
        if(implementingType != null)
        {
            dynamic obj = Activator.CreateInstance(implementingType);
    
            dynamic result = obj.SampleInterfaceMethod();
    
            Console.WriteLine(result);
        }
    }
    

    Try using some of this. I was able to call into that object and then get the result from that method back.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.