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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:52:18+00:00 2026-05-23T21:52:18+00:00

I’m writting a c# code generator. In some place I must cast return type

  • 0

I’m writting a c# code generator. In some place I must cast return type of Linq’s method like WhereSelectListIterator to IEnumerable type. How can I do it?

Scenario:
I have an instance of a List named aList. I write following expression in a Textbox:

aList.Where(item => item.StartsWith("s")).Select(item => item.ToUpper()).ToList().Count

I need to write an application to evaluate my expression. I know that i must write an C# code generator to evaluate my expression.
If I evaluate above expression directly it is works. But suppose that I have following scenario :

public Interface IInterface
{
}
public class MyClass:IInterface
{
    public int Id = 10;
    public IInterface GetInstance()
    {
        return new MyClass();
    }
}

public class Program
{
    public static void Main()
    {
        var myClass = new MyClass();
        myClass.GetInstance().Id.ToString();
    }
}

When I use Id property after calling GetInstance() method it cause to CLR raise an exception because IInterface hasn’t Id property. I need to cast return type of GetInstance() to MyClass first then use Id property.
How can I do this cast dynamically.
One solution is using dynamic object.

var myClass = new MyClass();
dynamic x = myClass.GetInstance();
dynamic y = myClass.Id;
y.ToString();

But dynamic object has problem with Extension methods(ex : Where(), Take() , …). What can I do?

  • 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-23T21:52:19+00:00Added an answer on May 23, 2026 at 9:52 pm

    In answer to the question you ask in the title, The result of Enumerable<T>.Where(Predicate) implements IEnumerable<T>, just cast it, no need to do anything fancy. See here: Enumerable..Where(TSource) Method (IEnumerable(TSource), Func(TSource, Boolean))

    As for the rest, why are you trying to generate C# code? If you’re trying to evaluate a code snippet that was entered at runtime, why not use the C# compiler to compile it? I Googled for “C# programmatic compilation”, and the first result was this Microsoft support page: How to programmatically compile code using C# compiler

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
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 French site that I want to parse, but am running into

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.