How to initialize a list of a generic class - c#

I have a generic class
public class MetadataDifference<T>
{
public T NewMetadata { get; private set; }
public T OldMetadata { get; private set; }
// Other useful properties
public MetadataDifference(T newMetadata, T oldMetadata)
{
NewMetadata = newMetadata;
OldMetadata = oldMetadata;
}
}
I have wrapper class which has a list of MetadataDifference<> as a property.
This doesn't work:
The type or namespace name 'T' could not be found
Code:
public class DifferencesResult
{
public IEnumerable<MetadataDifference<T>> MetadataChanges { get; set; }
// other fields
}
How can I initialize a list of a generic object? Is it possible?

Either enclosing type must be opened generic:
public class DifferencesResult<T>
{
public IEnumerable<MetadataDifference<T>> MetadataChanges { get; set; }
// other fields
}
or you should use methods instead of property:
public class DifferencesResult
{
public IEnumerable<MetadataDifference<T>> GetMetadataChanges<T>();
private void SetMetadataChanges<T>(IEnumerable<MetadataDifference<T>> value)
// other fields
}
In C#, you can't hold generic property in non-generic class.
It depends on what result you want to achieve.

Here you should use a closed type, for example:
public class DifferencesResult
{
public IEnumerable<MetadataDifference<string>> MetadataChanges { get; set; }
// other fields
}
As you cannot have a generic property in a non-generic class.

You could either close it:
public class DifferencesResult
{
public IEnumerable<MetadataDifference<{sometype}>> MetadataChanges { get; set; }
// other fields
}
or use dynamic:
public class DifferencesResult
{
public IEnumerable<MetadataDifference<dynamic>> MetadataChanges { get; set; }
// other fields
}

Define an interface that doesn't have any generic types which MetadataDifference implements to provide untyped access to the underlying object:
public interface IMetadataDifference
{
object NewMetadata { get; }
object OldMetadata { get; }
}
public interface IMetadataDifference<out T> : IMetadataDifference
{
new T NewMetadata { get; }
new T OldMetadata { get; }
}
public class MetadataDifference<T> : IMetadataDifference<T>
{
object IMetadataDifference.NewMetadata { get { return NewMetadata; } }
object IMetadataDifference.OldMetadata { get { return OldMetadata; } }
public T NewMetadata { get; private set; }
public T OldMetadata { get; private set; }
// Other useful properties
public MetadataDifference(T newMetadata, T oldMetadata)
{
NewMetadata = newMetadata;
OldMetadata = oldMetadata;
}
}
public class DifferencesResult
{
public IEnumerable<IMetadataDifference> MetadataChanges { get; set; }
// other fields
}

Related

How to use Generic Delegate to access Derived Class Members?

Let me preface by saying I am new to coding and C# is my first language
I have a interface:
public interface ITDXInput <TSCContext, TContext, TWaferContainer> : ITDXInput
{
TSCContext SCContext { get; set; }
TContext ToolContext { get; set; }
TWaferContainer WaferContainer { get; set; }
}
This class implements the interface:
public class CIMTDXInput : ITDXInput<CIMSCContext, CIMToolContext, CIMWaferContainer>
{
public CIMWaferContainer WaferContainer { get; set; }
public CIMSCContext SCContext { get; set; }
public CIMToolContext ToolContext { get; set; }
}
The members of this class inherit from the following interfaces:
public class CIMSCContext : ISCContext
{
public string PROCESSING_END_TIME { get; set; }
public string PRODUCT_NAME { get; set; }
public string QUANTITY_IN { get; set; }
public string LOT_OWNER { get; set; }
public string FW_VERSION { get; set; }
}
public class CIMToolContext : IToolContext
{
public string LOT { get; set; }
public string TDX_MULTI_FILES { get; set; }
public string PROCESSING_START_TIME { get; set; }
public string WITHIN_UNIT_AXIS_DIRECTION { get; set; }
}
public class CIMWaferContainer : IWaferContainer
{
public CIMWaferContext WaferContext = new CIMWaferContext();
public List<CIMWaferImage> ImageList = new List<CIMWaferImage>();
public List<CIMWaferDieResults> WaferResultList = new List<CIMWaferDieResults>();
}
I have a delegate handling class:
public class KlarfTemplateDelegateHandler
{
public static Dictionary<string, Delegate> HandlerMap = new Dictionary<string, Delegate>();
public static Dictionary<string, Delegate> DefectListHandlerMap = new Dictionary<string, Delegate>();
delegate string TemplateDelegate<T,U,V>(ITDXInput<T, U, V> cimInput);
delegate string DefectListTemplateDelegate(CIMTDXInput cimInput, List<CIMKlarfDefectList> list);
static KlarfTemplateDelegateHandler()
{
HandlerMap["#DATA.TOOL_CONTEXT.PROCESSING_END_TIME_DATE#"] = new TemplateDelegate<ISCContext,IToolContext, IWaferContainer>(ProcessEndDate);
HandlerMap["#DATA.TOOL_CONTEXT.PROCESSING_END_TIME_TIME#"] = new TemplateDelegate(ProcessEndTime);
}
private static string ProcessEndDate<T,U,V>(ITDXInput<T,U,V> cimInput)
{
DateTime dateTime = DateTime.Parse(cimInput.ToolContext.PROCESSING_END_TIME);
//return cimInput.CIMSCContext.PROCESSING_END_TIME.Split(' ')[0];
return dateTime.ToString("MM-dd-yyyy");
}
}
The issue is that for the function ProcessEndDate, it tells me that 'T' does not contain a definition for "PROCESSING_END_TIME_TIME". I tried to replace TSCContext inside ITDXInput with ISCContext as the generic for the ITDXInput's TSCContext but it's localized.
Is there anything I can do to be able to get the function to see that whatever ITDXInput I pass into the ProcessEndDate function will have the PROCESSING_END_TIME_TIME definition?
I am going through this exercise because I would like to reuse that delegatehandler class for other types of TDXInput
One approach would be to constrain the ProcessEndDate method. A minimal example of this would be to have this interface:
public interface ISCContext
{
string PROCESSING_END_TIME { get; set; }
}
with two different classes that implement it:
public class ClassA : ISCContext
{
public string PROCESSING_END_TIME { get; set; } = "A:" + DateTime.UtcNow.ToString();
}
public class ClassB : ISCContext
{
public string PROCESSING_END_TIME { get; set; } = "B:" + DateTime.UtcNow.ToString();
}
To let the compiler know what to expect in a generic method, you would use the keyword where to make an interface constraint. For clarity, I made a new method with only 1 generic argument. You could, of course, have multiple arguments and corresponding constraints to define expectations for T, U and V but this gives you the basic idea.
private static void showProcessingEndTime<T>(T item) where T : ISCContext
{
Console.WriteLine(item.PROCESSING_END_TIME);
}
Here's a Microsoft article explaining the many ways Generic Type Constraints may be used.
Test code
static void Main(string[] args)
{
var a = new ClassA();
showProcessingEndTime(a);
var b = new ClassB();
showProcessingEndTime(b);
}

Set interface us a properties c#

I've created three interfaces - IUser, ICostCenter, IDepartment. IUser and IDepartment are properties of ICostCenter.
public interface ICostCenter
{
IDepartment Department { get; set; }
User.IUser CostCenterHead { get; set; }
}
The class implementing ICostCenter will be used in the DbContext for my db.
public class tblCostCenter : WorkingInterface.Interface.Organization.ICostCenter
{
public tblCostCenter()
{
this.ID = Guid.NewGuid().ToString();
}
public string ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
private WorkingInterface.Interface.User.IUser CostCenterHead { get; set; }
public string UserID { get; set; }
[ForeignKey("UserID")]
public Model.tblUser UserObject
{
get { return (Model.tblUser)this.CostCenterHead; }
set { this.CostCenterHead = value; }
}
private WorkingInterface.Interface.Organization.IDepartment Department { get; set; }
public string DepartmenID { get; set; }
[ForeignKey("DepartmenID")]
public Model.tblDepartment DepartmentObject
{
get { return (Model.tblDepartment)this.Department; }
set { this.Department = (Model.tblDepartment)value; }
}
}
I'm getting an error because I made the IUser and IDepartment properties private. How can I properly implement the interface? I want these properties to be hidden.
The interface is declared explicitly as public, that means that all the members declared in that interface MUST be public
So you need to declare both of these properties as public:
public WorkingInterface.Interface.User.IUser CostCenterHead { get; set; }
public WorkingInterface.Interface.Organization.IDepartment Department { get; set; }
NOTE: these have been declared as implicit implementations, so they are available to all contexts. If you want to hide them from general use, you can use explicit implementation.
Because you tried to use private accessibility, I assume you generally want to hide these properties from view, you can do this and still honor the interface contract by using explicit implementation syntax:
WorkingInterface.Interface.User.IUser ICostCenter.CostCenterHead { get; set; }
WorkingInterface.Interface.Organization.IDepartment ICostCenter.Department { get; set; }
They are still public but can only be accessed by first casting the object to the specific interface type explicitly.
tblCostCenter obj = new tblCostCenter();
...
// this wont work:
Console.WriteLine(obj.CostCenterHead);
// instead you will have to cast the object first
Console.WriteLine((obj as ICostCenter)).CostCenterHead);
// or if the local variable was an ICostCenter
ICostCenter costObj = obj;
Console.WriteLine(costObj.CostCenterHead);
...
// Pattern matching can also be helpful in these scenarios
if (obj is ICostCenter cc)
Console.WriteLine(cc.CostCenterHead);
If you want to use explicit declaration in your class you could try this:
WorkingInterface.Interface.User.IUser ICostCenter.CostCenterHead { get; set; }
public string UserID { get; set; }
[ForeignKey("UserID")]
public Model.tblUser UserObject
{
get { return ((ICostCenter)this).CostCenterHead as Model.tblUser; }
set { ((ICostCenter)this).CostCenterHead = value; }
}
WorkingInterface.Interface.Organization.IDepartment ICostCenter.Department { get; set; }
public string DepartmenID { get; set; }
[ForeignKey("DepartmenID")]
public Model.tblDepartment DepartmentObject
{
get { return ((ICostCenter)this).Department as Model.tblDepartment; }
set { ((ICostCenter)this).Department = value; }
}
this only works if:
Model.tblDepartment : IDepartment
Model.tblUser : User.IUser

How to get rid of downcast in this case?

I broke my head over this already. So here is the situation. I have two types of documents with similar properties. High-level (base-level) properties (Name, Date) are required in one place, "Rows" are required to create specific document to send to another system. How it is implemented now:
Data classes:
public abstract class BaseDocument
{
public string Name { get; set; }
public DateTime Date { get; set; }
}
public abstract class BaseDocument<TRowType> : BaseDocument
{
public abstract List<TRowType> Rows { get; set; }
}
public class DocumentTypeOne : BaseDocument<RowTypeOne>
{
public override List<RowTypeOne> Rows { get; set; }
}
public class DocumentTypeTwo : BaseDocument<RowTypeTwo>
{
public override List<RowTypeTwo> Rows { get; set; }
}
public class RowTypeOne
{
public int Cost { get; set; }
}
public class RowTypeTwo
{
public int Change { get; set; }
}
ProcessorClass:
public class DocumentsProcessor
{
public void ProcessDocument(BaseDocument doc)
{
switch (doc)
{
case DocumentTypeOne t1:
ProcessDocumentTypeOne((DocumentTypeOne)doc);
break;
case DocumentTypeTwo t2:
ProcessDocumentsTypeTwo((DocumentTypeTwo)doc);
break;
default:
throw new ArgumentException($"Unhandled type {nameof(doc)}");
}
}
public void ProcessDocumentTypeOne(DocumentTypeOne docOne)
{
// specific actions
}
public void ProcessDocumentsTypeTwo(DocumentTypeTwo docTwo)
{
// other specific actions
}
}
I know that downcasting is not good. But I have no ideas how to change it.
I can make base class with generic parameter but then I'll lost ability to work with only base-level properties. And this will require to rewrite class that return List.
What's the way to solve it? And is it needed to be solved?
You might wanna use interfaces.
public interface IBaseDocument
{
string Name { get; set; }
DateTime Date { get; set; }
}
public interface IDocumentWithRows<T>
{
List<T> Rows { get; set; }
}
public class DocumentTypeOne: IBaseDocument, IDocumentWithRows<RowTypeOne>
{
string IBaseDocument.Name { get; set; }
DateTime IBaseDocument.Date { get; set; }
List<RowTypeOne> IDocumentWithRows<RowTypeOne>.Rows { get; set; }
}
public class DocumentProcessor
{
public void ProcessDocument(IBaseDocument doc)
{
switch (doc)
{
case DocumentTypeOne docTypeOne:
ProcessDocumentTypeOne(docTypeOne);
break;
case DocumentTypeTwo docTypeTwo:
ProcessDocumentTypeTwo(docTypeTwo);
break;
}
}
}

How to serialize to XML generic classes?

I have these interfaces:
public interface IParameter
{
string Name { get; }
object UntypedValue { get; set; }
}
public interface IValidationPolicy<T>
{
bool Validate(T toValidate);
T Default();
}
A parameter base class
[Serializable]
public abstract class ParameterBase : IParameter
{
public abstract string Name { get; protected set; }
public abstract object UntypedValue { get; set; }
}
A parameter concrete class (I have more but them are quite similar):
public class Parameter<T, V> : ParameterBase where V : IValidationPolicy<T>
{
[XmlAttribute("Name")]
public override string Name { get; protected set; }
[XmlIgnore]
protected V validation_policy_;
[XmlElement("AnyValidation", Type = typeof(AnyValidation<>))]
[XmlElement("MultiOptionsValidation", Type = typeof(MultiOptionsValidation<>))]
[XmlElement("RangeValidation", Type = typeof(RangeValidation<>))]
[XmlElement("TextValidation", Type = typeof(TextValidation))]
public V Validation
{
get
{
return validation_policy_;
}
}
[XmlIgnore]
protected T value_;
[XmlElement("Value")]
public T Value
{
get
{
return value_;
}
set
{
if (validation_policy_.Validate(value))
{
value_ = value;
}
}
}
[XmlIgnore]
public object UntypedValue
{
get
{
return Value;
}
set
{
throw new NotImplementedException();
}
}
}
And an XMLParameter class:
public class XMLParameter : INotifyPropertyChanged
{
public string Description { get; set; }
public int PasswordLevel { get; set; }
public bool Enabled { get; set; }
public ParameterBase Parameter { get; set; }
}
How can I serialize and deserialize a list of XMLParameters?
In particular I have problem on serializing the IParameter objects.
Since the interface is not serializable as first attempt I created a base abstract class ParameterBase and derive the Parameter from it.
But when I try to serialize it in a test method:
var validation = new RangeValidation<int>() { MinValue = 1, MaxValue = 6 };
var parameter = new Parameter<int, RangeValidation<int>>();
parameter.Initialize("NumberOfTrays", validation);
parameter.Value = 6;
XElement par = validation.ToXElement<Parameter<int, RangeValidation<int>>>();
I got an exception: Error at reflection of type 'ConfigurableLibray.Parameter'2[System.Int32,ConfigurableLibray.RangeValidation'1[System.Int32]]'
The inner exception says that ConfigurableLibray.Parameter'2[T,V] is not supported
What am I doing wrong?
Thanks in advance for any suggestion!
I solved implementing manually the serialization and deserialization of the classes using reflection.

Generic parameter problem

I have a business model which consists of a parent/child relationship (Identifier/IdentifierValue) and also some snapshot classes which look the same (IdentifierSnapshot/IdentifierValueSnapshot).
I am trying to create an extension method which will work on an enumeration of either Identifier or IdentifierSnapshot, but I just can't work out what to put in the extension method where I have inserted the XXX placeholder.
//Interfaces
public interface IIdentifier<TIdentifierValue>
where TIdentifierValue : IIdentifierValue
{
string Code { get; }
IEnumerable<TIdentifierValue> GetValues();
}
public interface IIdentifierValue
{
string Code { get; }
string Value { get; }
}
//Main classes
public class Identifier : IIdentifier<IdentifierValue>
{
public string Code { get; set; }
public IEnumerable<IdentifierValue> GetValues();
}
public class IdentifierValue : IIdentifierValue
{
public string Code { get; set; }
public string Value { get; set; }
}
//Snapshots
public class IdentifierSnapshot : IIdentifier<IdentifierValueSnapshot>
{
public string Code { get; set; }
public IEnumerable<IdentifierValueSnapshot> GetValues();
}
public class IdentifierValueSnapshot : IIdentifierValue
{
public string Code { get; set; }
public string Value { get; set; }
}
public static IdentifierExtensions
{
public static IEnumerable<XXX> ByCode<XXX>(this IEnumerable<XXX> instance, string code)
{
return instance.Where(x => string.Compare(code, x.Code, true) == 0);
}
}
I think this would do it:
public static IEnumerable<T> ByCode<T,Z>(this IEnumerable<T> instance, string code)
where T:IIdentifier<Z>
where Z:IIdentifierValue

Categories