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 7893085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:02:03+00:00 2026-06-03T07:02:03+00:00

Please read the entire question. I have a unique situation with several constraints that

  • 0

Please read the entire question. I have a unique situation with several constraints that I’d like to solve.

In my code I have an expression tree which is compiled to a Predicate<System.Type>. My goal is to load an assembly without locking it (it is the output assembly of the project, being rebuilt constantly), apply this predicate on the list of its types and get back a list of the resulting type names:

// this is what I need:
return assembly.GetTypes().Where(t => predicate(t)).Select(t => t.FullName);

This assembly should be loaded in another appdomain, because I want to unload it as soon as I get the information I require.

Here is where it gets tricky. There are several problems I’m facing:

If I load the assembly in another appdomain and simply return an array of all of its types, so that I could apply the predicate back in my main appdomain, as soon as the Types are marshalled back to my main appdomain I get a FileNotFoundException, stating that the this assembly is not found. This makes sence, because the assembly is only being loaded in another appdomain I created. Loading it also in the main appdomain will defeat the purpose.

If, alternatively, I try to pass the predicate into the other appdomain, to apply it there and get back an array of strings (full type name), I get a SerializationException: "Cannot serialize delegates over unmanaged function pointers, dynamic methods or methods outside the delegate creator's assembly.", because the predicate is a Dynamic Method (compiled from an expression tree).

Loading it in the primary appdomain would have none of this problems, but since it’s impossible to unload a loaded assembly without unloading the entire appdomain, as soon as the assembly would change (after rebuild), any attempt to load an assembly with the same name would result in an exception.

Context:
I am building a plugin for ReSharper called Agent Mulder. The idea behind the plugin is to analyze DI/IoC Container registrations in your solution, and help ReSharper figure out the usage of types registered via a DI Container (you can watch a short video of how it works here).

For the most part, analyzing container registration is straightforward – I only have to detect enough information to know which concrete types are affected. In this example with Castle Windsor: Component.For<IFoo>().ImplementedBy<Foo>() the resulting type is obvious, so is AllTypes.FromThisAssembly().BasedOn<IFoo>() – giving me just enough information to guestimate the concrete types that will be affected by this line. However, consider this registration in Castle Windsor:

container.Register(Classes
    .FromAssemblyInDirectory(new AssemblyFilter(".").FilterByName(an => an.Name.StartsWith("Ploeh.Samples.Booking")))
    .Where(t => !(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Dispatcher<>)))
    .WithServiceAllInterfaces());

(source)

Here the information depends on a predicate that will only be evaluated at runtime.

Since all I can do is statically analyze this, I have in my hand the ReSharper’s AST (called PSI in ReSharper) representation of the lambda expression from the Where clause. I can convert this AST into a LINQ expression tree, then compile it into a delegate.

My idea was is to load the output assembly (determined by the FromAssembly* descriptor) via reflection, and apply this delegate on the assembly’s types in order to get the type names (I only need the names). This will have to be re-evaluated every time the assembly changes, too (I’m not concerned at this point about performance).

In conclusion, unless someone could recommend a better way of determine the types affected by the predicate, I’d like to know how to do this with reflection (unfortunately I hadn’t considered other metadata readers, because I’d have to somehow convert the lambda expression AST to a predicate of different data type, and I don’t know if there exists a 1-to-1 conversion).

Thank you for reading. This question will have a 500 point bounty when it becomes available.

  • 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-03T07:02:04+00:00Added an answer on June 3, 2026 at 7:02 am

    You need to load the assembly to get the Type instances, so a separate AppDomain seems like the right solution.

    So, you need to get the predicate Expression into that AppDomain, which means you have to serialize / deserialize it.

    This requirement is becoming more and more frequent for various reasons. I was looking at this because I wanted to squirt Linq to Entities expressions across a WCF service.

    Fortunately, there are a few existing implementations.

    I found this one: CodePlex – Expression Tree Serializer


    I’ve just tested it with Types, and this works:

    Expression<Func<Type,bool>> predicate =
      t => ( !t.IsGenericType && t.Name == "Int32" );
    
    var s = new ExpressionSerialization.ExpressionSerializer();
    var xml = s.Serialize( predicate );
    
    var p = s.Deserialize( xml ) as Expression<Func<Type, bool>>;
    var f = p.Compile();
    
    Console.WriteLine( "Int32: " + f( typeof( int ) ) ); // true
    Console.WriteLine( "String: " + f( typeof( string ) ) ); // false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read somewhere that <img> element behaves like both. If correct, could someone please
Please read this entire question before you try to mark this as too controversial.
I'd like to have vim highlight entire lines that match certain patterns. I can
Please read my update at the end of question after reading the answers: I'm
please read carefully because my english is not good, and the question has not
Before jumping on me, please read my question carefully :D. I know there are
Here is what I got so far. Please read the comment in the code.
This is my homework, but please read my problem description first. I have to
I am curious to know why this is happening. Please read the code example
I read my teacher's code, but I don't understand. Could anyone please tell me

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.