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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:35:17+00:00 2026-05-31T23:35:17+00:00

Just a quickie before the weekend rolls in… I have a Method with the

  • 0

Just a quickie before the weekend rolls in…

I have a Method with the following signature, that I need to invoke:

public interface IObjectProvider<T>
{
    T Get(Predicate<T> condition);
}

This will provide me with a T from whatever kind of source, that meets the predicate criteria.

Now, this has to be called from a context where all I have is the following:

//the actual predicate that's going to be evaluated
var predicate = predicateProperty.GetValue(invocation.InvocationTarget, null);

//The type that should go into the call as type param
Type relationTargetType = relationDefinition.RelatedType;

As you might guess, the compiler won’t let me use the predicate variable as parameter. What I need to do is convert this object into a Predicate, but Generic type params must be compile-time-constant, so this won’t work.

I’ve started messing around with this, but no success so far:

Type genericPredicateType = typeof(Predicate<>);
Type specificPredicateType= genericPredicateType.MakeGenericType(relationTargetType);
Convert.ChangeType(predicate, specificPredicateType)

How on earth can I mash this up?

EDIT: I thought this was a rather use-case-agnostic question, but obviously I was wrong. So, since there is such a fuss as to what I do, what I have and why and whatnot, here’s a lot more background info. I am trying to resolve relations between objects in a Proxy (Castle Dynamic Proxy). The following Snippet should explain the kind of relation I want to depict:

public class Order
{
    public virtual int Id { get; set; } // OR-Mapped
    public virtual DateTime OrderDate { get; set; } // OR-Mapped

    [RelatedObject(typeof(Address), "DeliveryAddressPredicate")]
    public virtual Address DeliveryAddress { get; set; }

    public Predicate<Address> DeliveryAddressPredicate
    {
        get { return new Predicate<Address>(a => OrderDate >= a.ValidFrom && OrderDate <= a.ValidTo); }
    }
}

public class Address
{
    public virtual DateTime ValidFrom { get; set; } // OR-Mapped
    public virtual DateTime ValidTo { get; set; } // OR-Mapped

    //Not OR-Mapped
    [RelatedList(typeof(Order), "OrdersPredicate")]
    public virtual IList<Order> Orders { get; set; }

    public Predicate<Order> OrdersPredicate
    {
        get { return new Predicate<Order>(o => o.OrderDate >= ValidFrom && o.OrderDate <= ValidTo); }
    }

To sum it up, this is supposed to become a Fuzzy OR-Mapping, meant to extend NHibernate in a project or two.

How did I mean to get this to work? The address is proxied, and when a call to a property with one of my CustomAttributes is made, i use DynamicProxy’s IInterceptor interface to resolve the relation. The main problem is that this resolving has to happen in the IInterceptor.Intercept() Method which has only one Param (see here), and I have no generic type param available. So, in the end it all boils down to a simple .Net question again: I have a Type stored in a variable and a Method that has to be called with a parameter generic of the aforesaid type…

Sorry for any mistakes made (like calling var a Type – man that was a rough one), it’s been quite a day 😉

  • 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-05-31T23:35:18+00:00Added an answer on May 31, 2026 at 11:35 pm

    You have some IObjectProvider<T>. If T is a type know at compile-time, you can just use a cast. For example, if T was Foo:

    IObjectProvider<Foo> provider = …;
    var predicate = (Predicate<Foo>)predicateProperty.GetValue(
        invocation.InvocationTarget, null);
    Foo item = provider.Get(predicate);
    

    EDIT: It seems you don’t know T at compile time. This means you have two options:

    1. Use dynamic:

      object provider = …
      object predicate = predicateProperty.GetValue(
          invocation.InvocationTarget, null);
      object item = ((dynamic)provider).Get((dynamic)predicate);
      
    2. Use reflection:

      object provider = …;
      object predicate = predicateProperty.GetValue(
          invocation.InvocationTarget, null);
      var getMethod = provider.GetType().GetMethod("Get");
      object item = getMethod.Invoke(provider, new[] { predicate });
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a quickie ... I have the following ID generator strategy for one of
Just how much slower are events? I have written a streaming XML parser (that
Just wondering what the best (fastest execution time) method for translating the following SQL
Just a quickie Do i need to install crystal report full version to run
Just a quick question. I have an NSArray that is generated in my applicationDidFinishLaunching
Just a quickie, i have an xml resource in res/values/integers.xml <?xml version=1.0 encoding=utf-8?> <resources>
Just what the title says, I need to change the password for an existing
Just looking for the first step basic solution here that keeps the honest people
just a quick question: I am a CS undergrad and have only had experience
Just a quickie.. If I set on an alert document.title to ALERT per say.

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.