I have an employee object:
public class CreateEmployee
{
public string FirstName { get; set; }
public string LastName { get; set; }
[Import(AllowDefault=true)]
public ExtendEmployee ExtendEmployee { get; set; }
}
public class ExtendEmployee
{
public string Id { get; set; }
}
I want to extend this ExtendEmployee during runtime using MEF.
[Export]
public class ExtendCreateEmployee : ExtendEmployee
{
public decimal Salary { get; set; }
}
My Question is: If I dont define this [Export], is there a way to
import the base class "ExtendEmployee" instead of the default null for
the import.
I considered decorating the base class with [Export] attribute but in that case, the import will consider both the classes and I have to filter the inherited class. This can be fine if there is a way to either choose from the base class or the inherited class.
Thanks
Related
Here is an example of declaring a single parameter generics class with single constraints.
public class TestClass<T> : ITestClass<T> where T : Teacher
I am looking for a way to declare the TestClass with multiple constraints. Where I will be able to use an interface for different classes.
How can I do something like this?...
public class TestClass<T> : ITestClass<T> where T : Teacher, Student, new()
My classes look like
public class Teacher{
public string Name { get; set; }
public string Designation { get; set; }
}
public class Student{
public string Name { get; set; }
public int Roll { get; set; }
public string Department { get; set; }
}
I have a model class that is extended from the realm object. In some cases, I use this model as both realm model and POST operations. Currently, the realm IList properties unable to support setter options. Is there any option or way to achieve this?
Here is my current code:
[JsonObject]
public class Product : RealmObject, IProduct
{
[MapTo("name")]
[JsonProperty("name")]
public string Name { get; set; }
[MapTo("skuDetails")]
[JsonProperty("skuDetails")]
public IList<SkuDetail> SkuDetails { get; }
}
My requirement :
[JsonObject]
public class Product : RealmObject, IProduct
{
[MapTo("name")]
[JsonProperty("name")]
public string Name { get; set; }
[MapTo("skuDetails")]
[JsonProperty("skuDetails")]
public IList<SkuDetail> SkuDetails { get; set; }
}
you need to derive your class according your need.
this is totally possible like the code below:
public interface ITest
{
IList<object> SkuDetails { get; }
}
public class OutTest : ITest
{
public IList<object> SkuDetails { get; set; }
}
Please note that extending property method is supported however opposite of it is not.
Ex:
public interface ITest
{
IList SkuDetails { get; set; }
}
public class OutTest : ITest
{
public IList<object> SkuDetails { get; }
}
is not possible.
I have a base class like this-ish:
public class Baseclass
{
public string Id { get; set; }
public string Type { get; set; }
public string Name { get; set; }
}
...and many classes that inherit these properties, like this-ish:
public class Thing: Baseclass
{
public string Size{ get; set; }
public string Color{ get; set; }
public string Smell{ get; set; }
}
Now, I don't want to serialize all of these properties (mvc/jsonresult), so I use [JsonIgnore] on the properties of a class I want to exclude, and that works fine. The problem is that I don't want to serialize all the inherited properties for a class either. I've asked around and gotten the following answer:
Ex: I don't want to serialize the inherited Id from Baseclass in Thing.
I should make Id in Baseclass virutal:
public virtual string Id { get; set; }
and add the following to the Thing class:
[JsonIgnore]
public override string Id { get; set; }
...but this doesn't work, I'm afraid. I can get around it rebuilding the class hierarchy. but I would prefer a simpler solution. Any suggestions as to why this solution didn't work or alternatives to exclude certain inherited properties?
Suppose I have 2 entities one is UserMaster another is ProjectMaster, now there are few common properties in both the entities and I need to apply same data annotations and attributes on both set of common properties which I do using metadata classes, now my question is there any way that I have a common metadata class for all this common properties across entities,so that I don't need apply data annotation and attributes for common properties again and again and if yes then how to achieve this.
You can create a base metadata class and the other metadata class derive from the base and add some other properties.
Example:
In the below example I supposed you can't derive Class2 from Class1 so I have inheritance only between metadata classes, otherwise you can simply have inheritance between your model classes.
[MetadataType(typeof(Class1Metadata))]
public class Class1
{
public string Id { get; set; }
public string Name { get; set; }
}
[MetadataType(typeof(Class2Metadata))]
public class Class2
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Class1Metadata
{
[Display(Name="Id1")]
public string Id { get; set; }
[Display(Name = "Name1")]
public string Name { get; set; }
}
public class Class2Metadata:Class1Metadata
{
[Display(Name = "Description2")]
public string Description { get; set; }
}
I want to inherit some properties that are the same in 2 classes using interface or abstract class; I don't want to declare variable in child classes.
Is it possible to do this?
Parent Class:
interface IError
{
DateTime GETDATE { get; set; }
string ERROR_NUMBER { get; set; }
string ERROR_SEVERITY { get; set; }
string ERROR_STATE { get; set; }
string ERROR_PROCEDURE { get; set; }
string ERROR_LINE { get; set; }
string ERROR_MESSAGE { get; set; }
}
Child :
public class Business
{
public Business()
{
this.Id = -1;
this.Message = "";
}
public int Id { get; set; }
public string Message { get; set; }
}
Another child:
public class DbTools
{
//Another Class
}
I need parent properties in this two classes, but if change code like below, I should use another variable that I don't want to:
public class Business : IError
{
private DateTime m_GetDATE;//I don't want to declare another variable
public DateTime GETDATE
{
get { return m_GetDATE; }
set { GETDATE = m_GetDATE; }
}
}
Sounds like you want an abstract base class:
public abstract class ErrorBase : IError
{
public DateTime GETDATE { get; set; }
//...ditto all the other IError properties
}
Then you can just inherit that:
public class Business : ErrorBase
{
// all the ErrorBase properties are inherited
}
You could also make the base class properties virtual just in case you need to override them in the derived classes at any point.
The other thing you might consider is composing rather than inheriting. It doesn't seem all that natural that Business and DbTools should be inheriting from the same base class, so instead have a (non-abstract) ErrorBase class that implements IError and have Business and DbTools have an Error property:
public class ErrorBase : IError
{
public DateTime GETDATE { get; set; }
//...ditto all the other IError properties
}
public interface IHaveError
{
ErrorBase Error { get; set; }
}
public class Business : IHaveError
{
public ErrorBase { get; set; }
}
As a bonus, you could make IHaveError actually be IHaveErrors and have a List<ErrorBase> property allowing your objects to hold more than one error, which might be useful sometimes.