I have a set of rules which is defined by this interface:
public interface IRule<in T>
{
bool IsBroken(T dataObject);
}
Sample implementations:
class Rule1:IRule<DateTime>
{
public bool IsBroken(DateTime dataObject)
{
throw new NotImplementedException();
}
}
class Rule2:IRule<int>
{
public bool IsBroken(int dataObject)
{
throw new NotImplementedException();
}
}
I have another class which passess back the lsit of rules as below:
class RulesCollection
{
public IEnumerable<IRule<what>> GetAllRules()
{
yield return new Rule1();
yield return new Rule2();
}
}
The issue 'am facing here is- how will pass back the rule1 & rule2 into IEnumerable<IRule<T>>, since T is going to be different for various rules it does't allow me to do so. Is there a way to get over this issue?
Using .NET 4.
Depending on the meaning of the return value of IsBroken, you could do something like:
public class RuleWrapper<T> : IRule<object>
{
private readonly IRule<T> inner;
public RuleWrapper(IRule<T> inner)
{
this.inner = inner;
}
public bool IsBroken(object obj)
{
return obj is T && this.inner.IsBroken((T)obj);
}
}
and then:
public IEnumerable<IRule<object>> GetAllRules()
{
yield return new RuleWrapper<DateTime>(new Rule1());
yield return new RuleWrapper<int>(new Rule2());
}
Related
I created a class that inherits from KeyedByTypeCollection and extends it.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.keyedbytypecollection-1?view=netframework-4.7.2
KeyedByTypeCollection only has a Find method which returns null if no item is found. I prefer a TryGetValue method so I added one.
internal class TypeCollection<V> : KeyedByTypeCollection<V>
{
public T ValueOrDefault<T>() where T : V
{
if (!Contains(typeof(T)))
{
return default(T);
}
return (T)this[typeof(T)];
}
public bool TryGetValue<T>(out T value) where T : V
{
if (!Contains(typeof(T)))
{
value = default(T);
return false;
}
value = (T)this[typeof(T)];
return true;
}
}
The thing is there is no reason for inheritance. I just want to extend an existing class. I started with these two extension methods
internal static class KeyedByTypeCollectionExtensions
{
public static T ValueOrDefault<T>(this KeyedByTypeCollection<V> collection) where T : V
{
if (!collection.Contains(typeof(T)))
{
return default(T);
}
return (T)collection[typeof(T)];
}
public static bool TryGetValue<T>(this KeyedByTypeCollection<V> collection, out T value) where T : V
{
if (!collection.Contains(typeof(T)))
{
value = default(T);
return false;
}
value = (T)collection[typeof(T)];
return true;
}
}
but how do I setup these extension methods? What do I have to set for the generic type V?
You will have to define V.
public static T ValueOrDefault<T,V>(this KeyedByTypeCollection<V> collection) where T : V
and
public static bool TryGetValue<T,V>(this KeyedByTypeCollection<V> collection, out T value)
where T : V
It will work nice with the TryGetValue, because the compiler will know which types are used, but for the ValueOrDefault you will have to set both of the types, which is a bit ugly.
Lets consider the following classes:
public class A { }
public class B : A { }
Then usage can be:
var myCollection = new KeyedByTypeCollection<A>();
myCollection.Add(new A());
myCollection.Add(new B());
myCollection.TryGetValue(out B b); // <-- Nice! :)
b = myCollection.ValueOrDefault<B,A>(); // <-- Ugly :(
Say that I have a series of classes:
abstract class MyClass { }
class MyFirstClass : MyClass { }
class MySecondClass : MyClass { }
class MyThirdClass : MyClass { }
I want to do something based on a configurable list of these derived class types, so I want to store the chosen class's Types in a list. I know I could create a List<Type>, but I could theoretically add any class to that list. On the other hand, a List<MyClass> would be a list of instances of these classes, rather than a list of the types themselves. I could also create an enum with one value that corresponds to each derived type, and have a factory method to create the correct one as needed, but that's at least two more places I'd have to update when I added MyFourthClass.
Is there a way to do something like new List<typeof(MyClass)>() = new[] { typeof(MyFirstClass), typeof(MyThirdClass)}? Does the very fact I'm asking this question imply a problem with my design?
What you want is a generic list of types (List<Type>) but like you said, you can insert any type there. The solution I can give you is to implement your own List of types from MyClass, for instance:
class TypeMyClassList : IList<Type>
{
private readonly List<Type> _list = new List<Type>();
private bool CheckType(Type type)
{
return type.IsSubclassOf(typeof (MyClass)) || typeof (MyClass) == type;
}
public IEnumerator<Type> GetEnumerator()
{
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(Type item)
{
if (CheckType(item))
_list.Add(item);
else
throw new InvalidOperationException("You can't add other types than derived from A");
}
public void Clear()
{
_list.Clear();
}
public bool Contains(Type item)
{
return _list.Contains(item);
}
public void CopyTo(Type[] array, int arrayIndex)
{
_list.CopyTo(array, arrayIndex);
}
public bool Remove(Type item)
{
return _list.Remove(item);
}
public int Count
{
get { return _list.Count; }
}
public bool IsReadOnly { get { return false; } }
public int IndexOf(Type item)
{
return _list.IndexOf(item);
}
public void Insert(int index, Type item)
{
if (!CheckType(item))
throw new InvalidOperationException("You can't insert other types than derived from A");
_list.Add(item);
}
public void RemoveAt(int index)
{
_list.RemoveAt(index);
}
public Type this[int index]
{
get
{
return _list[index];
}
set
{
Insert(index, value);
}
}
}
Then you could do thinks like this that you want:
var typeMyClassList = new TypeMyClassList
{
typeof(MyClass),
typeof(MyClassA),
typeof(MyClassB)
};
The bad thing is that it will allows to do this in compilance time (the error will be raised on runtime):
var typeMyClassList = new TypeMyClassList
{
typeof(MyClass),
typeof(MyClassA),
typeof(MyClassB),
typeof(string)
};
There's no way to do this with static, compile-time type checking. Your best bet is to go with a solution like Raul OtaƱo's in which you do your checks at runtime.
Why can't you do this? The reason is that C# lacks static metaclass types. A metaclass is the class of a class. In other words, the instances of a metaclass are themselves classes. If C# had metaclasses, you could say something like IList<MyClassMeta> (or perhaps the syntax would be IList<meta MyClass> and the compiler would only allow you to pass MyClass (or its subclasses) as "instances", e.g.,
IList<meta MyClass> list;
list.Add(MyClass);
I've been wanting this functionality for a long time, but I don't expect it any time soon.
Having code as below, getting a compile error - "... does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'".
How to implement the non-generic version of GetEnumerator?
public class Giraffe { }
public class Pigeon { }
public class Sample : IEnumerable<Giraffe>, IEnumerable<Pigeon>
{
IEnumerator<Giraffe> IEnumerable<Giraffe>.GetEnumerator()
{
return null;
}
IEnumerator<Pigeon> IEnumerable<Pigeon>.GetEnumerator()
{
return null;
}
}
Try:
public class Pets :IEnumerable, IEnumerable<Giraffe>, IEnumerable<Pigeon>
{
IEnumerator<Giraffe> IEnumerable<Giraffe>.GetEnumerator()
{
return null;
}
IEnumerator<Pigeon> IEnumerable<Pigeon>.GetEnumerator()
{
return null;
}
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
if I understand your problem correctly, here is a sample how you can implement non-generic Enumerator in your class
public class Sample : IEnumerable<Giraffe>, IEnumerable<Pigeon>
{
IEnumerator<Giraffe> IEnumerable<Giraffe>.GetEnumerator()
{
return null;
}
IEnumerator<Pigeon> IEnumerable<Pigeon>.GetEnumerator()
{
return null;
}
IEnumerator IEnumerable.GetEnumerator()
{
return null; //your logic for the enumerator
}
}
since the generic IEnumerable<T> inherits non-generic IEnumerable so implementing IEnumerable.GetEnumerator would define an implementation for the same.
you may additionally declare the class as public class Sample : IEnumerable, IEnumerable<Giraffe>, IEnumerable<Pigeon> for more clarity
example
public class Sample : IEnumerable, IEnumerable<Giraffe>, IEnumerable<Pigeon>
{
IEnumerator<Giraffe> IEnumerable<Giraffe>.GetEnumerator()
{
return null;
}
IEnumerator<Pigeon> IEnumerable<Pigeon>.GetEnumerator()
{
return null;
}
IEnumerator IEnumerable.GetEnumerator()
{
return null; //your logic for the enumerator
}
}
I have a custom ConfigurationElementCollection called EnvironmentElementCollection to configure my different environments within a class library I'm writing. I have a static property in a class to get a collection of all the environments that are listed in that section. There is a property of the contained items (EnvironmentElement) that indicates the environment is a "login" environment. My goal is to be able to filter the collection with Linq to get only the "login" environments, but the Linq query always returns null. I implemented IEnumerable using this tutorial, but I can't figure out what I'm doing wrong.
Configuration Classes
public class EnvironmentSection : ConfigurationSection {
private static ConfigurationPropertyCollection properties;
private static ConfigurationProperty propEnvironments;
static EnvironmentSection() {
propEnvironments = new ConfigurationProperty(null, typeof(EnvironmentElementCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
properties = new ConfigurationPropertyCollection { propEnvironments };
}
protected override ConfigurationPropertyCollection Properties {
get {
return properties;
}
}
public EnvironmentElementCollection Environments {
get {
return this[propEnvironments] as EnvironmentElementCollection;
}
}
}
public class EnvironmentElementCollection : ConfigurationElementCollection, IEnumerable<EnvironmentElement> {
private static ConfigurationPropertyCollection properties;
public EnvironmentElementCollection() {
properties = new ConfigurationPropertyCollection();
}
protected override ConfigurationPropertyCollection Properties {
get {
return properties;
}
}
public override ConfigurationElementCollectionType CollectionType {
get {
return ConfigurationElementCollectionType.BasicMap;
}
}
public EnvironmentElement this[int index] {
get {
return (EnvironmentElement)base.BaseGet(index);
}
}
public EnvironmentElement this[string name] {
get {
return (EnvironmentElement)base.BaseGet(name);
}
}
protected override string ElementName {
get {
return "add";
}
}
protected override ConfigurationElement CreateNewElement() {
return new EnvironmentElement();
}
protected override object GetElementKey(ConfigurationElement element) {
var elm = element as EnvironmentElement;
if (elm == null) throw new ArgumentNullException();
return elm.Name;
}
//for implementing IEnumerable
public new IEnumerator<EnvironmentElement> GetEnumerator() {
int count = base.Count;
for (int i = 0; i < count; i++) {
yield return (EnvironmentElement)base.BaseGet(i);
}
}
}
public class EnvironmentElement : ConfigurationElement {
private static ConfigurationPropertyCollection properties;
private static ConfigurationProperty propLoginEnabled;
public EnvironmentElement() {
propLoginEnabled = new ConfigurationProperty("loginEnabled", typeof(bool), null, ConfigurationPropertyOptions.None);
properties = new ConfigurationPropertyCollection { propLoginEnabled };
}
[ConfigurationProperty("loginEnabled")]
public bool LoginEnabled {
get {
return (bool)base[propLoginEnabled];
}
}
}
And here is my class to get environments:
public class Environment {
//Works fine to get all environments
public static EnvironmentElementCollection All {
get {
EnvironmentSection dls = ConfigurationManager.GetSection("environments") as EnvironmentSection;
return dls.Environments;
}
}
//Returns null
public static EnvironmentElementCollection Login {
get {
EnvironmentSection dls = ConfigurationManager.GetSection("environments") as EnvironmentSection;
EnvironmentElementCollection envs = dls.Environments.Where<EnvironmentElement>(e => e.LoginEnabled == true) as EnvironmentElementCollection;
return envs;
}
}
}
So I can't tell which part is breaking, if my implementation of IEnumerable is bad, or my Linq query, or something else.
Why can I not cast a List<ObjBase> as List<Obj>? Why does the following not work:
internal class ObjBase
{
}
internal class Obj : ObjBase
{
}
internal class ObjManager
{
internal List<Obj> returnStuff()
{
return getSomeStuff() as List<Obj>;
}
private List<ObjBase> getSomeStuff()
{
return new List<ObjBase>();
}
}
Instead I have to do this:
internal class ObjBase
{
}
internal class Obj : ObjBase
{
}
internal class ObjManager
{
internal List<Obj> returnStuff()
{
List<ObjBase> returnedList = getSomeStuff();
List<Obj> listToReturn = new List<Obj>(returnedList.Count);
foreach (ObjBase currentBaseObject in returnedList)
{
listToReturn.Add(currentBaseObject as Obj);
}
return listToReturn;
}
private List<ObjBase> getSomeStuff()
{
return new List<ObjBase>();
}
}
I get the following error in Visual Studio 2008 (shortened for readability):
Cannot convert type 'List' to 'List' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
Thanks.
You can use Cast and ToList extension methods from System.Linq to have this in one line.
Instead of
internal List<Obj> returnStuff()
{
return getSomeStuff() as List<Obj>;
}
do this:
internal List<Obj> returnStuff()
{
return getSomeStuff().Cast<Obj>().ToList();
}
I can only describe the "problem" from a Java view, but from what little I know this aspect is the same in both C# and Java:
A List<ObjBase> is not a List<Obj>, because it could contain an ObjBase object which is not a Obj object.
The other way around a List<Obj> can not be cast to a List<ObjBase> because the former guarantees to accept an Add() call with a ObjBase argument, which the latter will not accept!
So to summarize: even though a Obj is-a ObjBase a List<Obj> is not a List<ObjBase>.
Please look at the following questions:
.NET Casting Generic List
Why does this generic cast fail?
Covariance my friend.
Look at http://blog.t-l-k.com/dot-net/2009/c-sharp-4-covariance-and-contravariance
list.ConvertAll looks tempting but has 1 big disadvantage: it will create a whole new list. This will impact performance and memory usage especially for big lists.
With a bit more effort you can create a wrapper list class that keeps the original list as an internal reference, and convert the items only when they are used.
Usage:
var x = new List<ObjBase>();
var y = x.CastList<ObjBase, Obj>(); // y is now an IList<Obj>
Code to add to your library:
public static class Extensions
{
public static IList<TTo> CastList<TFrom, TTo>(this IList<TFrom> list)
{
return new CastedList<TTo, TFrom>(list);
}
}
public class CastedList<TTo, TFrom> : IList<TTo>
{
public IList<TFrom> BaseList;
public CastedList(IList<TFrom> baseList)
{
BaseList = baseList;
}
// IEnumerable
IEnumerator IEnumerable.GetEnumerator() { return BaseList.GetEnumerator(); }
// IEnumerable<>
public IEnumerator<TTo> GetEnumerator() { return new CastedEnumerator<TTo, TFrom>(BaseList.GetEnumerator()); }
// ICollection
public int Count { get { return BaseList.Count; } }
public bool IsReadOnly { get { return BaseList.IsReadOnly; } }
public void Add(TTo item) { BaseList.Add((TFrom)(object)item); }
public void Clear() { BaseList.Clear(); }
public bool Contains(TTo item) { return BaseList.Contains((TFrom)(object)item); }
public void CopyTo(TTo[] array, int arrayIndex) { BaseList.CopyTo((TFrom[])(object)array, arrayIndex); }
public bool Remove(TTo item) { return BaseList.Remove((TFrom)(object)item); }
// IList
public TTo this[int index]
{
get { return (TTo)(object)BaseList[index]; }
set { BaseList[index] = (TFrom)(object)value; }
}
public int IndexOf(TTo item) { return BaseList.IndexOf((TFrom)(object)item); }
public void Insert(int index, TTo item) { BaseList.Insert(index, (TFrom)(object)item); }
public void RemoveAt(int index) { BaseList.RemoveAt(index); }
}
public class CastedEnumerator<TTo, TFrom> : IEnumerator<TTo>
{
public IEnumerator<TFrom> BaseEnumerator;
public CastedEnumerator(IEnumerator<TFrom> baseEnumerator)
{
BaseEnumerator = baseEnumerator;
}
// IDisposable
public void Dispose() { BaseEnumerator.Dispose(); }
// IEnumerator
object IEnumerator.Current { get { return BaseEnumerator.Current; } }
public bool MoveNext() { return BaseEnumerator.MoveNext(); }
public void Reset() { BaseEnumerator.Reset(); }
// IEnumerator<>
public TTo Current { get { return (TTo)(object)BaseEnumerator.Current; } }
}
I think you are misunderstanding the cast you are trying to do. You are thinking that you are changing the type of the object that is stored in the list, where you are actually trying to change the type of the list itself. It rather makes sense that you can't change the list itself as you have already populated it.
You might look at it as a list of a base class and then cast it when you are processing the list items, that would be my approach.
What is the purpose of this attempted cast?
C# currently does not support variance for generic types. From what I've read, this will change in 4.0.
See here for more information on variance in generics.
Linq has a ConvertAll method. so something like
list.ConvertAll<Obj>(objBase => objbase.ConvertTo(obj));
I'm not sure what else to suggest. I assume ObjBase is the base class, and if all ObjBase objects are Obj objects, i'm not sure why you would have the two objects in the first place. Perhaps i'm off the mark.
Edit: the list.Cast method would work better than the above, assuming they are castable to each other. Forgot about that until I read the other answers.
This is a major pain in C# - this is how generics were designed. List doesn't extend List, its just a completely different type. You can't cast or assign them to each other in any way, your only option is to copy one list to the other one.
Lazarus:
I thought that the compiler would realise that I wanted actions done on the objects of the list and not that I was trying to cast the list itself.
Some more information:
public abstract class ObjBase
{
}
internal interface IDatabaseObject
{
}
public class Obj : ObjBase, IDatabaseObject
{
}
internal interface IDatabaseObjectManager
{
List<ObjBase> getSomeStuff();
}
public class ObjManager : IObjManager
{
public List<Obj> returnStuff()
{
return getSomeStuff().Cast <Customer>().ToList<Customer>();
}
private List<ObjBase> getSomeStuff()
{
return new List<ObjBase>();
}
}
Now client code outside of this DLL can go:
ObjManager objM = new ObjManager();
List listOB = objM.returnStuff();
I'm going to be creating several Obj and ObjManager types for this part (O/RM) of the application.
(Darn comment block ran out of characters! :-)
Here is how I fixed the Conversion from a
list<SomeOtherObject>
to a
object
and then to a
List<object>
https://stackoverflow.com/a/16147909/2307326