Converting Method to Generic Method? - c#

I have created a Method shown below,
public BOEod CheckCommandStatus(BOEod pBo, IList<string> pProperties)
{
pBo.isValid = false;
if (pProperties != null)
{
int Num=-1;
pBo.GetType().GetProperty(pProperties[0].ToString()).GetValue(pBo, null);
if (ifIntegerGetValue(pBo.GetType().GetProperty(pProperties[0].ToString()).GetValue(pBo, null).ToString(), out Num))
{
if (Num == 1)
pBo.isValid = true;
}
}
return pBo;
}
I need to convert this method, in such a way that it should accept all Type of Objects(now i am accepting only Objects of type "BOEod").
As i am newbie to .Net so don no exactly how to use Generics. Can i accomplish this using Generics.?
Solution Something like this:
public T CheckCommandStatus<T>(T pBO, Ilist<string> pProperties){..}
Here Main thing is i need to change the Property of the Passed Object(pBO) and return it.

You would need BOEod to implement an interface which defines IsValid.
You would then add a generic constraint to your method to only accept objects implementing that interface.
public interface IIsValid
{
bool IsValid{get;set;}
}
....
public class BOEod : IIsValid
{
public bool IsValid{get;set;}
}
....
public T CheckCommandStatus<T>(T pBO, IList<string> pProperties)
where T : IIsValid{..}

public BOEod CheckCommandStatus<T>(T pBo, IList<string> pProperties) where T : IBOEod
{
pBo.isValid = false;
if (pProperties != null)
{
int Num = -1;
string propValue = pBo.GetType().GetProperty(pProperties[0].ToString()).GetValue(pBo, null).ToString();
if (ifIntegerGetValue(propValue, out Num))
{
if (Num == 1)
pBo.isValid = true;
}
}
return pBo;
}
public interface IBOEod
{
bool IsValid { get; set; }
}
All the types you want to pass to this method must implement the IBOEod interface.

Related

Check if class is a subclass of specific generic

I have the following classes:
public class HtSecurePage : UserControl, IDisposable
{
}
public class HtSecureInstancePage<T1> : HtSecurePage
{
}
public partial class NormalPage : HtSecurePage
{
}
public partial class InstancePage : HtSecureInstancePage<ZlsManager>
{
}
To check if NormalPage is a subClass of HtSecurePage I use the following pattern.
if (typeof(NormalPage).BaseType == typeof(HtSecurePage))
{
}
If I use this pattern against InstancePage, it is not working.
if (typeof(InstancePage).BaseType == typeof(HtSecureInstancePage<>))
{
}
I need to know if a Type is a direct subClass of HtSecurePage or HtSecureInstancePage<>. (It's important not to check against HtSecureInstancePage<ZlsManager>!) The Type T1 is unknown.
Below function check your class' sub-class the same type supplied class. If types is generic, check operation is executed over generic type definition.
Method usage
bool isInherited = CheckIsDirectlyInherited(typeof(TestAbstract), new[] {typeof(SecondLevelAbstractClass), typeof(FirstLevelAbstract)});
Method
bool CheckIsDirectlyInherited(Type obj, Type[] baseTypes)
{
if (obj.BaseType == null)
return false;
var objGenericDefinition = obj.BaseType;
if (objGenericDefinition.IsGenericType)
{
objGenericDefinition = objGenericDefinition.GetGenericTypeDefinition();
}
foreach (Type baseType in baseTypes)
{
var baseTypeDefinition = baseType;
if (baseTypeDefinition.IsGenericType)
baseTypeDefinition = baseType.GetGenericTypeDefinition();
if (objGenericDefinition == baseTypeDefinition)
return true;
}
return false;
}
is a direct subClass of HtSecurePage
I think you already know how to do it
Console.WriteLine(typeof(HtSecureInstancePage<ZlsManager>).BaseType == typeof(HtSecurePage));
is a direct subClass of HtSecureInstancePage<>
To check it you can use something like this:
static bool IsDirectSubclassOfRawGeneric(Type parent, Type toCheck)
{
return toCheck.BaseType.IsGenericType && parent == toCheck.BaseType.GetGenericTypeDefinition();
}
...
Console.WriteLine(IsDirectSubclassOfRawGeneric(typeof(HtSecureInstancePage<>), typeof(InstancePage)));

Check if generic parameter of a class implements an interface

I have a following generic class:
public class SearchResult<T>
{
public int ResultCount { get; set; }
public IEnumerable<T> Result { get; set; }
}
I also have a Bird class, which implements IFlyble interface:
public class Bird : IFlyable
{
public void Fly() {}
}
public interface IFlyable
{
void Fly();
}
I also have a variable res of type object.
How do I check if res is a SearchResult<IFlyable> ?
I tryied this way:
if (res.GetType().IsAssignableFrom(typeof(SearchResult<IFlyable>)))
{
///
}
And this way:
if(res is SearchResult<IFlyable>)
{
///
}
But it does not seems to work.
The problem you are having is probably due to the fact that SearchResult<Bird> is not convertible to SearchResult<IFlyable> because SearchResult<T> is invariant in T.
C# only admitís generic type variance in interfaces and delegates. You need to define an ISearchResult<> interface that is covariant in its generic type.
In your case, if it’s accepatable that T Is only used as an output you could define such interface as follows:
public interface ISearchResult<out T>
{
int ResultCount { get; }
IEnumerable<T> Result { get; }
}
And now a ISearchResult<Bird> is a ISearchResult<IFlyable> because you’ve given the compiler enough information so that it can verify that the conversion is in fact safe
You can also try this using reflection, which also works and no need to create another interface.
static void Main()
{
var sr = new SearchResult<Bird>();
Console.WriteLine(IsSearchResultIFlyable(sr.GetType())
? "sr is SearchResult<IFlyable>"
: "sr is Not SearchResult<IFlyable>");
Console.ReadLine();
}
public static bool IsSearchResultIFlyable(Type t)
{
if (!t.IsGenericType) return false;
if (t.GetGenericTypeDefinition() != typeof(SearchResult<>)) return false;
var gr = t.GetGenericArguments();
return gr.Length == 1 && typeof(IFlyable).IsAssignableFrom(gr[0]);
}

How to find out if a type implements generics base class

Using the example below... how can I find out whether a property is of a type implementing generics class Foo?
public class Foo<TBaz>
{
}
public class Bar
{
public Foo<int> FooInt { get; set; }
public Foo<string> FooString { get; set; }
public double SomeOther { get; set; }
public int GetFooCount()
{
return typeof(Bar).GetProperties().Where(p => p.GetType().IsGenericType).Count();
}
}
If I wanted to find Foo<int>, it would be easy, but how can I find out if it contains Foo<int>, Foo<double> etc...?
I have written the bit of GetFooCount() I have so far...
Thanks
return typeof(Bar).GetProperties().Where(p => p.PropertyType.IsGenericType
&& p.PropertyType.GetGenericTypeDefinition() == typeof(Foo<>)).Count();
Note: this won't automatically work for class NonGenericSubtype : Foo<Blah> {...}, nor will it work for class GenericSubtype<T> : Foo<T> {...} - if you need to handle those, it gets more fun.
For the more general case, you would need something that uses recursion on the type:
public static int GetFooCount()
{
return typeof(Bar).GetProperties()
.Count(p => GetFooType(p.PropertyType) != null);
}
private static Type GetFooType(Type type)
{
while(type != null)
{
if (type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(Foo<>))
return type.GetGenericArguments()[0];
type = type.BaseType;
}
return null;
}
Note this also answers "now how do I find T?"

Class properties of certain generic type cast into generic type to execute methods

First I would like to apologize for the messy title. I'm not quite sure how to put it into words so I will describe the situation.
I'm writing a comparison engine for our product, that is capable of comparing different products like this:
public abstract class ComparableProduct
{
public ComparableProperty<decimal> Weight { get; set; }
public ComparableProperty<decimal> Width { get; set; }
public ComparableProperty<decimal> Height { get; set; }
public ComparableProperty<decimal> Depth { get; set; }
public bool IsBetterThan(ComparableProduct target){}
}
Actual products are derived from ComparableProduct, such as Screen : ComparableProduct, which adds property
ComparableProperty<decimal> Dimension { get; set; }
This means that I can have a class Laptop which has a property Keyboard, Screen, StorageDevice... etc etc, which are all derived from ComparableProduct.
Those have in turn comparable properties like this:
public abstract class ComparableProperty<T> where T : IComparable<T>
{
T Value { get; set; }
public ComparisonType ComparisonType { get; set; }
public bool IsBetterThan(T target)
{
if(ComparisonType == ComparisonType.GreaterThan)
return Value.CompareTo(target) >= 0;
return Value.CompareTo(target) <= 0;
}
public bool IsBetterThan(IEnumerable<T> targets)
{
foreach(var target in targets)
{
if (ComparisonType == ComparisonType.SmallerThan && Value.CompareTo(target) >= 0)
return false;
if (ComparisonType == ComparisonType.GreaterThan && Value.CompareTo(target) <= 0)
return false;
}
return true;
}
}
I haven't tested these, by all logic they should work. The trouble I'm having... is with the IsBetterThan method in ComparableProduct. The expected functionality is that on the top class (say, Laptop), is looped through its ComparableProduct properties and called IsBetterThan for the other copy and those will loop through their subproperties... In addition, all the ComparableProduct ComparableProperty properties are checked with IsBetterThan with the other class' equivalent value.
Anyway, here's what I have, and you can see the problem I have immediately.
public bool IsBetterThan(ComparableProduct target)
{
foreach(var property in GetType().GetProperties().Where(x => x.PropertyType == typeof(ComparableProduct)))
{
var compareTo = target.GetType().GetProperty(property.Name).GetValue(target, null) as ComparableProduct;
var local = property.GetValue(this, null) as ComparableProduct;
if(local != null && !local.IsBetterThan(compareTo))
return false;
}
foreach(var property in GetType().GetProperties().Where(x => x.PropertyType == typeof(ComparableProperty<>)))
{
var compareTo = target.GetType().GetProperty(property.Name).GetValue(target, null) as ComparableProperty<>;
var local = property.GetValue(this, null) as ComparableProperty<>;
if(local != null && !local.IsBetterThan(compareTo))
return false;
}
}
As you can see, I'm trying to cast it to ComparableProperty<>, which means it's missing the generic type. However, I'm not quite sure how to get the generic type of the involved property.
Also, if there's a better way of doing it... I will take any tips I can, but this is the first half decent way of doing it that came to my mind.
EDIT:
Spoke too soon. When I try to enumerate the properties in ComparableProduct's IsBetterThan like this:
foreach(var property in GetType().GetProperties())
{
var t = property.GetType().GetInterfaces();
if (!property.GetType().GetInterfaces().Contains(typeof(IComparableProperty))) continue;
var compareTo = target.GetType().GetProperty(property.Name).GetValue(target, null) as IComparableProperty;
var local = property.GetValue(this, null) as IComparableProperty;
if (local == null) continue;
if(!local.IsBetterThan(compareTo))
return false;
}
Then it appears it can't find IComparableProperty in the interfaces. I have gone through the main methods that may contain it... but the only interfaces t contains are ICustomAttributeProvider, _MemberInfo, _PropertyInfo and ISerializable.
EDIT 2:
I have solved this by falling back on string comparison on
if (property.PropertyType.Name != "ComparableProperty`1") continue;
And after changing T to ComparableProperty and IEnumerable to IEnumerable> the whole comparison works perfectly.
You can create a non-generic interface, and then work with it:
public interface IComparableProperty
{
bool IsBetterThan(object target);
bool IsBetterThan(IEnumerable targets);
}
public abstract class ComparableProperty<T>: IComparableProperty where T : IComparable<T>
{
T Value { get; set; }
public ComparisonType ComparisonType { get; set; }
public bool IsBetterThan(T target)
{
if (ComparisonType == ComparisonType.GreaterThan)
return Value.CompareTo(target) >= 0;
return Value.CompareTo(target) <= 0;
}
public bool IsBetterThan(IEnumerable<T> targets)
{
foreach (var target in targets)
{
if (ComparisonType == ComparisonType.SmallerThan && Value.CompareTo(target) >= 0)
return false;
if (ComparisonType == ComparisonType.GreaterThan && Value.CompareTo(target) <= 0)
return false;
}
return true;
}
bool IComparableProperty.IsBetterThan(object target)
{
return IsBetterThan((T) target);
}
bool IComparableProperty.IsBetterThan(IEnumerable targets)
{
return IsBetterThan((IEnumerable<T>) (targets));
}
}
EDIT 0:
Did you try to use this method Type.IsAssignableFrom(Type) like this:
foreach (var property in GetType().GetProperties())
{
if (!typeof(IComparableProperty).IsAssignableFrom(property.PropertyType)) continue;
var compareTo = target.GetType().GetProperty(property.Name).GetValue(target, null) as IComparableProperty;
var local = property.GetValue(this, null) as IComparableProperty;
if (local == null) continue;
return local.IsBetterThan(compareTo);
}

How do I override the equals operator == for an interface in C#?

I have defined the following interface:
public interface IHaveAProblem
{
string Issue { get; set; }
}
And here is the implementation of IHaveAProblem:
public class SomeProblem : IHaveAProblem
{
public string Issue { get; set; }
public override bool Equals(object obj)
{
SomeProblem otherObj = obj as SomeProblem;
if (otherObj == null)
{
return false;
}
return this.Issue == otherObj.Issue;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public static bool operator ==(SomeProblem rhs, SomeProblem lhs)
{
// Null check
if (Object.ReferenceEquals(rhs, null) || Object.ReferenceEquals(lhs, null))
{
if (Object.ReferenceEquals(rhs, null) && Object.ReferenceEquals(lhs, null))
{
// Both are null. They do equal each other
return true;
}
// Only 1 is null the other is not so they do not equal
return false;
}
return rhs.Equals(lhs);
}
public static bool operator !=(SomeProblem rhs, SomeProblem lhs)
{
// Null check
if (Object.ReferenceEquals(rhs, null) || Object.ReferenceEquals(lhs, null))
{
if (Object.ReferenceEquals(rhs, null) && Object.ReferenceEquals(lhs, null))
{
// Both are null. They do equal each other
return false;
}
// Only 1 is null the other is not so they do not equal
return true;
}
return !rhs.Equals(lhs);
}
}
When I use the object, I can get the correct results for the == compare:
SomeProblem firstTest = new SomeProblem()
{
Issue = "Hello World"
};
SomeProblem secondTest = new SomeProblem()
{
Issue = "Hello World"
};
// This is true
bool result = firstTest == secondTest;
However, when I try to compare the interfaces, it is doing a memory compare rather than the operator == on SomeProblem:
IHaveAProblem firstProblem = new SomeProblem()
{
Issue = "Hello World"
};
IHaveAProblem secondProblem = new SomeProblem()
{
Issue = "Hello World"
};
Is it possible to have the interface use the == on SomeProblem rather than a memory compare?
I know I can do a firstProblem.Equals(secondProblem) and get the proper results. However, I am creating a framework and I will not know how it is used in the end. I thought == would work correctly.
The operator == is static. You cannot define static methods for interfaces in C#. Also, for all operators at least one of the argument types needs to be of the same type as the class it is defined in, therefore: No operator overloading for interfaces :(
What you CAN do is use an abstract class instead - and define the operator there. Again, the operator may NOT be virtual (since static methods cannot be virtual...)
[Edited, reason see comment.]
I konw, this is an old question, but all examples provided show how to compare two class instances, and no one points out how to compare two interface instances.
In some cases, this is the DRYest way to compare interfaces.
public interface IHaveAProblem
{
string Issue { get; set; }
}
public class IHaveAProblemComparer : IComparer<IHaveAProblem>, IEqualityComparer<IHaveAProblem>
{
public int Compare(IHaveAProblem x, IHaveAProblem y)
{
return string.Compare(x.Issue, y.Issue);
}
public bool Equals(IHaveAProblem x, IHaveAProblem y)
{
return string.Equals(x.Issue, y.Issue);
}
public int GetHashCode(IHaveAProblem obj)
{
return obj.GetHashCode();
}
}
Usage?
IHaveAProblemComparer comparer = new IHaveAProblemComparer();
List<IHaveAProblem> myListOfInterfaces = GetSomeIHaveAProblemObjects();
myListOfInterfaces.Sort(comparer); // items ordered by Issue
IHaveAProblem obj1 = new SomeProblemTypeA() { Issue = "Example1" };
IHaveAProblem obj2 = new SomeProblemTypeB() { Issue = "Example2" };
bool areEquals = comparer.Equals(obj1, obj2); // False
IIRC (and I could be wrong here), C# interfaces don't allow operator overloading.
But in this case that's okay. The == operator normally maps to reference equality. It sounds like you want value equality, and that means you want to force them to override the .Equals() (and consequently also .GetHashCode()) functions. You do that by having your interface inherit from IEquatable.
Have you tried implementing IComparable?
Like this:
public interface IHaveAProblem : IComparable
{
string Issue { get; set; }
}
And then in the implementation of the class:
public class SomeProblem : IHaveAProblem
{
public string Issue { get; set; }
...
public int CompareTo(object obj)
{
return Issue.CompareTo(((SomeProblem)obj).Issue);
}
}
Note that, this works only when you compare two instances of SomeProblem, but not any other implementations of the IHaveAProblem interface.
Not sure if there could occur a NullReferenceException.

Categories

Resources