ICollection<T> where T implements an interface - c#

Is there any way to do something like this?
public interface ISomething
{
}
public class SomethingThatImplementsISomething: ISomething
{
}
public class SomethingElse
{
public ICollection<ISomething> PropertyName{ get; set; }
}
I've tried it out, and it keeps failing. Any ideas?

ICollection<Models.Awardable> can not be converted to ICollection<IAwardable>.
What would happen if someone tried to add an IAwardable to the collection that wasn't a Models.Awardable?
Just instantiate a collection of IAwardable and add instances of Models.Awardable to it.

Make sure you have using System.Collections.Generic rather than just using System.Collections at the top of your compilation unit.
Edited to add:
Actually, this probably has to do with the fact that version of C# prior to 4.0 do not allow for covariance or contravariance for generics. In older versions, a generic field must be defined to use an exact data type, to avoid invalid reference type assignments.
Edited again:
Now that I see the actual error message that you are getting, the problem is not in the definition of the field, but in its use. You need to put an explicit cast in the assignment.

The auto-property
public ICollection<ISomething> PropertyName{ get; set; }
tries to create a backing-field of type ICollection<>, which will fail. Try something like:
public List<ISomething> PropertyName{ get; set; }

It sounds like what you're trying to do is this:
ICollection<SomethingThatImplementsISomething> collection = new List<SomethingThatImplementsISomething>();
somethingElse.PropertyName = collection;
If that's the case, then this is a generic variance issue. ICollection is not covariant in its element type. (Because it it were, you could now go somethingElse.PropertyName.Add(somethingDifferentThatsAlsoAnISomething); -- and you'd have added a SomethingDifferentThatsAlsoAnISomething to a list of SomethingThatImplementsISomething, which breaks type safety.)
You need to instantiate a collection of ISomethings:
ICollection<ISomething> collection = new List<ISomething>();
somethingElse.PropertyName = collection;
You can then add SomethingThatImplementsISomething objects to your heart's content:
somethingElse.PropertyName.Add(new SomethingThatImplementsISomething());

Related

What can I do with property .set and .get?

I have an interface
public interface IIntf { string Foo { get; set; } }
and a class
class MyClass : IIntf
{
public string Foo { get; private set; }
}
. Now I get the compiler error
'MyClass' does not implement interface member 'IIntf.Foo.set'.
'MyClass.Foo.set' is not public.
So it seems that Foo.set is its own entity somehow (and Foo.get analogously). Can I do anything with them? If yes, what?
E.g. I tried implementing the missing setter property by using Foo.set - it was even suggested by Visual Studio
but I did not find any syntax that works. When following the suggestion and trying to implement the Foo.set method (without corresponding get because I have that already), I get a different type of compiler error:
But since Visual Studio shows (and, at times, suggests while typing code) Foo.set and Foo.get, I assume that something can be done with them. Any ideas what? Maybe some nice way of redirecting methods to only the getter / only the setter? Or something else?
Dealing with the issues / questions that #Kjara has raised in the comments:
Properties are implemented using get and set methods behind
the scenes (by the compiler).
You cannot easily access / pass around these
methods (without reflection).
When define properties, you must
define the get and set at the same time (you can't define the
get on one line and the set on another).
As per #FrankM and #Dirk above - the confusion here appears to stem from the fact that the IDE implies (falsely) that you can define the getter and setter separately.
Like this
public class MyClass : IIntf
{
public string Foo { get; private set; }
string IIntf.Foo { get => Foo; set => Foo = value; }
}

Iterate all properties of an aggregate class and instantiate?

There's all kinds of really awesome info on how to iterate through all of the properties of a class. What I want to do, however, is iterate through them and instantiate each property as new in the constructor... The class is an aggregate class representing a collection of entity framework model objects/classes. I could instantiate each in turn but that is going to be huge and a problem for maintenance when I decide to add another table to the database...
public class ClsAggModel {
public ClsAggModel() {
foreach (PropertyInfo props in this.GetType().GetProperties()) {
props.SetValue = new props.GetType(); //problem here!
}
}
public clsItem1 pProp1{ get; set; }
public clsItem2 pProp2{ get; set; }
//...and so on
The problem is that the compiler doesn't seem to know what props is at this point:
The type or namespace name 'props' could not be found (are you missing
a using directive or an assembly reference?)
which seems kind of strange since I just i stantiated it in the loop signature. I could fill out each property and do the private variable and all that jazz but what good is reflection if you can't abuse it?
Any tips?
You can not create instance on base of .GetType() in this manner.
This will not work:
new props.GetType();
You should try using:
Activator.CreateInstance(props.GetType());
Based on already provided solutions your code should look like this:
public class ClsAggModel {
public ClsAggModel() {
foreach (PropertyInfo prop in this.GetType().GetProperties()) {
var newValue = Activator.CreateInstance(prop.GetType());
prop.SetValue(this, newValue);
}
}
public clsItem1 pProp1{ get; set; }
public clsItem2 pProp2{ get; set; }
Keep in mind that all properties types must allow dynamic creation, by providing a default constructor (it is automatically there, if other constructor is not defined).
Assuming you want to call a default created type use the activator like this
props.SetValue(this, Activator.CreateInstance(props.GetType()));
Note, this will still fail if there is no default constructor. Because you are using reflection your compiler can no longer check for this.
Note 2: Be vary carefully with abusing reflection. it's a sure way to shoot yourself in the foot sooner or later.
Update of course setvalue is a method, changed this

How could I declare list of generics with different and unknown types and How could I initialize a generic with a type only known at runtime?

This is a 2 part question.
Firstly
I have a class called ComponentList that looks like this:
public class ComponentList<T> : List<T> where T : Component
{
}
Now I want to create an empty list of typeless ComponentLists:
List<ComponentList<>> MasterList = new List<ComponentList<>>();
I get an error because the ComponentList wants a generic type specified even though I'm not trying to initialize any ComponentLists yet (just the MasterList that contains them). How would I declare the MasterList of ComponentLists without initializing any ComponentLists (as I plan to initialize them during runtime with types only known at runtime)? After all, the MasterList needs to contain ComponentLists of different generic types, not just one.
Secondly
I know this one has been asked before, but I can't seem to wrap my head around the concepts of any of the proposed solutions.
As you know, I have a List<> called MasterList which is a list of ComponentLists (a custom class). A ComponentList is a generic class of an undefined type (that is constrained to being a subtype of Component).
In the following example, I'm trying to check if a ComponentList generic type (referenced from MasterList) is the same as this class (the class the code's being called from).
if (MasterList[i].GetType() == typeof(ComponentList<this.GetType()>))
{
}
Problem is, the code is meant to be automatically called from unknown child classes, not from this parent class. So the ComponentList generic type needs to be compared with the type of the child class, not this base class. Hence the "this.GetType" inplace of what would be the hardcoded name of the actual base class.
The this.GetType() type passed in the ComponentList<> returns a "type expected" error apparently because GetType() returns compile time type, not runtime type (which is what I need).
So how could I get the runtime type? And if I can't, what would be the best alternative to accomplishing what I'm trying to do? (I've heard a bit about something called reflections that might help me but I really don't understand).
You cannot determine generic types at runtime. C# (all .Net, actually) is designed that way on purpose. The compiler treats a generic type virtually same way as an explicitly defined type.
Consider the following:
class MyGenericClass<T>
{
public static T MyProperty { get; set; }
}
class MyIntClass
{
public static int MyProperty { get; set; }
}
class UseThem
{
public void test()
{
// both are JIT'ed to be exactly the same machine code
var foo = MyGenericClass<int>.MyProperty;
var bar = MyOtherClass.MyProperty;
}
}
Thus, you must provide a type for the generic class so that JIT knows what to compile.
Possible Alternative
If all of the possible types which could possible end up being the generic type are similar (inherit from the same base class or implement a similar interface), then you can instantiate the generic class with an interface or base class as the generic type:
List<ComponentList<ComponentBase>> MasterList = new List<ComponentList<ComponentBase>>();
// -OR-
List<ComponentList<IMyComponent>> MasterList = new List<ComponentList<IMyComponent>>();
I have a hunch that you should be able to define a common interface or base class with a little creative refactoring. :)
I've run into this issue as well when I was trying to set up an entity-component system for a game written in C#. There's really isn't a way to store components as their actual types, you have to store them all as Components and cast them.
The way I have it set up is as a Dictionary<Type, List<Component>> as a private member of a ComponentManager class. The method that adds components is generic and checks if it's Type is contained in the Dictionary, so getting an IEnumerable<SpecificComponent> is as simple as:
public IEnumerable<T> EnumerateComponents<T>()
where T : Component
{
foreach (Component c in components[typeof(T)])
yield return (T)c;
}
(You'll also want to check that the dictionary contains typeof(T), that bit is built-in with a custom collection of mine that inherits from Dictionary to avoid exceptions in cases like this.)
Type safety is "guaranteed" as long as the dictionary is never modified outside of a generic method (and definitely not directly accessible from the outside). Not ideal, but it's fast enough where it will never be your bottleneck.
EDIT
Something to explore might be C# 4's dynamic keyword. I haven't looked into it much, but storing the components as a List<dynamic> might work better (or it may introduce way too much overhead), just something to think about.
I don't think what you are asking is possible, however maybe this is what you need.
public class ComponentA : Component { }
public class ComponentB : Component { }
public class Component { }
public class ComponentList<T> : List<T> where T : Component
{
}
class Program
{
static void Main(string[] args)
{
ComponentList<Component> MasterList = new ComponentList<Component>();
MasterList.Add(new ComponentA());
MasterList.Add(new ComponentB());
for (int i = 0; i < MasterList.Count; i++)
{
if (MasterList[i] is ComponentA)
{
}
}
}
}

Property of generic type in non-generic class

Intro
I'm working with the legacy code which contains two classes:
I have a class which stores its value of System.Object type.
(I named this class as DomainItem)
Its Identifier property refers to
enum which holds information what a type of DomainItem is (in the
context of business domain).
There is also a class which stores these
items as an Enumerable List. (DomainItems)
What's more:
I don't want to change these classes into generic. This code is very sensitive and not covered by tests.
In order to get DomainItem, I must get it from DomainItems.Items collection.
Code
The code for classes is equivalent as below:
public class DomainItem
{
public Identifier Identifier { get; set; } // Readonly in the "real" code
public object Value { get; set; }
}
public class DomainItems
{
public IEnumerable<DomainItem> Items { get; set; }
}
The question is
How can I extend these classes using generics, to resolve type of Value property in the compile time. Is it even possible?
Example case might be as following:
DomainItem price = new DomainItem { Value = 25.20d, Identifier = Identifier.Price };
// ....
double priceValue = price.ProperValue; // generic property of type T
Obviously, above code is conceptual and it shows what I want to achieve. Any suggestions how to resolve that? Is it even possible?
Edit
My idea is to create a new IEnumerable<DomainItem<T>> where the collection is populated from non-generic DomainItem objects. Since the type of DomainItem.Value is known, it should be possible to make such collection somehow.
There's no such thing as a generic property, but you could easily create a generic method:
public T GetValue<T>() { ... }
public void SetValue<T>(T value) { ... }
You could then check typeof(T) within the method to make sure that it was appropriate for your identifier, ideally having made the identifier read-only. (It would be better as a constructor argument - I wouldn't expect it to make any sense to have a domain item whose identifier changed over time.)
Alternatively, you could just make the type of the Value property dynamic instead of object, assuming you're using C# 4+ with .NET 4+. Then your example code would compile - but it would perform an implicit (dynamic) conversion to double at execution time. You wouldn't get much safety there, but it would compile...

Stipulating that a property is required in a class - compile time

Is there a way to stipulate that the clients of a class should specify a value for a set of properties in a class. For example (see below code), Can i stipulate that "EmploymentType" property in Employment class should be specified at compile time? I know i can use parametrized constructor and such. I am specifically looking for outputting a custom warning or error during compile time. Is that possible?
public class Employment
{
public EmploymentType EmploymentType {get; set;}
}
public enum EmploymentType
{
FullTime = 1,
PartTime= 2
}
public class Client
{
Employment e = new Employment();
// if i build the above code, i should get a error or warning saying you should specify value for EmploymentType
}
As cmsjr stated what you need to do is this:
public class Employment
{
public Employment(EmploymentType employmentType)
{
this.EmploymentType = employmentType;
}
public EmploymentType EmploymentType { get; set; }
}
This will force callers to pass in the value at creation like this:
Employment e = new Employment(EmploymentType.FullTime);
In the situation where you need to have a default constructor (like serialization) but you still want to enforce the rule then you would need some sort of state validation. For instance anytime you attempt to perform an operation on the Employment class you can have it check for a valid state like this:
public EmploymentType? EmploymentType { get; set; } // Nullable Type
public void PerformAction()
{
if(this.Validate())
// Perform action
}
protected bool Validate()
{
if(!EmploymentType.HasValue)
throw new InvalidOperationException("EmploymentType must be set.");
}
If you're looking throw custom compiler warnings, this is not exactly possible. I asked a similar question here Custom Compiler Warnings
You could achieve what you want to do by not having a default constructor, and instead defining a constructor that takes employment type as an argument. If someone attempted to instantiate the class using a parameter-less constructor, they would get a compile error.
EDIT code sample
public Employment(EmploymentType eType)
{
this.EmploymentType = eType;
}
OO principles dictate that an object should never be in an invalid state. So this should be a constructor parameter.
There is no way to indicate that a property is required at compile time.
You mentioned in your original post that you already know about doing this with constructors, which is definitely the right way to do it. I don't believe there's going to be any way to do what you want, even with attributes (which is normally how you would manipulate compiler warnings, etc). Since the object could be created and then passed to another method to set the parameter, it's not necessarily as obvious as "client has to specify value".
You could try and create a custom FxCop or VSTS static analysis rule, but I don't think you'll be able to do this with just the C# compiler.
Interface Icontrol
' Declare an interface.
Property MustHave() As String
End Interface
Then in your user control include
Public Class mycontrol
Inherits System.Web.UI.UserControl
Implements Icontrol
It makes this property required

Categories

Resources