c# - Working with an unknown number of unknown types - .NET -


O people, I have removed some of the complications of my needs that I need to know, the origin of it.

I want to send a collection of values ​​for a method, and within that method, I want to test the value against value, say, property of an entity Property will always be of the same type of value.

I also want to test if the value is zero, or the default value, it is clearly dependent on whether the value type is a reference type or not the value type.

Now, if all of the values ​​sent to the method are of the same type, then I can do it using generic, quite easily, like this:

 < Code> public static void testGenerics & LT; TValueType & gt; (List of & lt; TValueType & gt; values) {// Trial Null / Default foreach (Value in TValueType v) {if (EqualityComparer & LT; TValueType & gt; .Default.Equals (V, Default (TValueType)) {// do not value // (; TValueType & gt .Default.Equals (V, SomeOtherValueOfTValueType) EqualityComparer & lt) {// value equals // comapre against another value of the same type if} Else / value is default for zero or its type} else is not equal to}}}}  

My question is, how do I do the same thing, if different in my collection Type a Price includes.

My main concerns are successfully identifying zero or default values, and successfully recognizes that if each value has been passed, then some other value of the same type is equal.

Can I just get it by passing the type object? / Strong> I can not really use Equity Compilers because I can not use generic, because I'm going through a different type of unknown number .

Solution?

UPDATE

OK, searching around, I can use the following code to test the redundant / default in my scenario (from Taken):

  object default value = type. ISAVUAP? Catalyst. Composition (type): zero;  

I think it could work.

Now, how can I successfully compare two types of the same type without knowing my own types and reliably?

itemprop = "text">

is Object.Equals (object left, right object) equals provides the argument provided. Why do you avoid using it?

  1. Required: Definition is equals (object) and GetHashCode () <
  2. Optional: Implementation == and more! = Operator

    So as you see, if you trust the object. Ecloss (object left, object correct) , this equality will be the best solution depending on the strongly needed part of the implementation pattern.

    In addition, this will be the fastest option, because it only depends on the virtual methods. Otherwise you will be involved in some reflection. Public static Zero testGenerics (IList value) {foreach (value in object v) {if (ReferenceEquals (null, v)) {// v tap reference} Other {var types = v. GetType (); If you type (eg: Evaluate Type and Equals (V, activator. Create instance (type))) {// V is the default value of its value type} and {// v is a non-empty value of some reference type} }}}


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -