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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:22:21+00:00 2026-06-18T16:22:21+00:00

Activator.CreateInstance(p.PropertyType) == value is false Given a type T: public class ODataTestHelper { public

  • 0

Activator.CreateInstance(p.PropertyType) == value is false

Given a type T:

public class ODataTestHelper
        {
            public int DataId { get; set; }
            public string DataItem { get; set; }
        }

And a foreach over the properties:

public string BuildQuery<T>(T searchModel,int page, int pageSize)
{
  var type=typeof(T);
  var filters = new List<string>();
  foreach (var p in type.GetProperties())
  {
    var value=p.GetValue(searchModel,null);
    if(value == null || value.ToString().IsNullOrEmpty() || (p.PropertyType.IsValueType && Activator.CreateInstance(p.PropertyType) == value ))
        continue;
     filters.Add(string.Format("{0} eq {1}", p.Name, WrapTypeValue(p.PropertyType, value)));
  }
   if (filters.Any())
        {
            result += "&$filter=" + filters.Aggregate((s1, s2) => s1 + " and ");
        }
        return result;
 }

Given a test method

[TestMethod]
        public void BuildQuery_StringProperty_ItRendersTheFilter()
        {
            const string x = "foo";
            string expected =String.Format( "&$filter={0} eq '{1}'",LinqOp.PropertyOf(() => default(ODataTestHelper).DataItem).Name , x);
            var od = new ODataBinder("http://localhost/");
            var q = od.BuildQuery(new ODataTestHelper { DataItem = x}, 0, 0);
            Assert.IsTrue(q.Contains(expected), string.Format("{0} did not contain {1}", q, expected));

        }

The immediate window confirms that the default coming out is 0 and the value in the instance is 0. It also confirms that p.PropertyType.IsValueType

0 doesn’t == the call to Activator.CreateInstance(p.PropertyType) so it doesn’t skip the unintiailized DataId. This is the answer for creating a default value per How can I call default(T) with a type? and Create default instance of type but I can’t seem to == or .Equals to test for equality?

  • 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-18T16:22:22+00:00Added an answer on June 18, 2026 at 4:22 pm

    The simple answer here is that they are different objects. The == operator in this context checks to see if both expressions reference same object. While the value of the two objects may be the same, the objects are not the same object and therefore not equal.

    To establish if the two objects have the same value you should use:
    (<expression 1>).Equals(<expression2)
    and not <expression 1> == <expression 2>

    You may need to override the Equals method to make this work in C# as I see by default the method checks for the same condition as ==. See here.

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

Sidebar

Related Questions

Lets say I have this class class Child { public string FirstName { get;
I have following code: Type type = Type.GetTypeFromProgID(interopTypeName); dynamic obj = Activator.CreateInstance(type); string propertyName
How could I make a List in PowerShell 2? I've tried these: [activator]::createinstance(([type]'system.collections.generic.list`1').makegenerictype([string])) and
Is is possible to user Activator.CreateInstance() to instantiate a type given the Type.FullName and
Which Activator.CreateInstance overload function to call? I have a type returned from Type proxyType
Let's imagine I have instantiated generic type by Activator.CreateInstance. Is there any way to
I want to instantiate a class at runtime using _syncHelper = (ISyncHelper) Activator.CreateInstance(SBD.Syrius.Synchronisation, SyncHelper);
I want to use Activator.CreateInstance(string assemblyName,string typName) ( http://msdn.microsoft.com/en-us/library/d133hta4%28v=VS.100%29.aspx ) in my Windows Phone
I am trying to set a value to a Nested Property of Class dynamically
I want to set the initial size of a dictionary using Activator.CreateInstance, what is

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.