Ignore [ScriptIgnore] Attribute in Inheriting Class - c#

I have a base class where a property has a ScriptIgnore attribute:
public class Base
{
public int Id { get; set; }
[ScriptIgnore]
public virtual string Name { get; set; }
}
I also have another class which inherits from the Base class:
public class Inherting : Base
{
public override string Name { get; set; }
}
I tried overriding the Name property but it is still not being serialized. Is there a way to override the ScriptIgnore attribute (ignore it in the inherting class)?

I tried overriding the Name property but it is still not being
serialized. Is there a way to override the ScriptIgnore attribute
(ignore it in the inherting class)?
Use ScriptIgnore's ApplyToOverrides property and set it to false:
[ScriptIgnore(ApplyToOverrides = false)]
public virtual string Name { get; set; }
Reference: ScriptIgnoreAttribute.ApplyToOverrides Property

Related

Attribute that will mark all non nullable fields as Required

I have dto with several fields
For example
public class SupplierModel
{
public string SupplierNameNormalized { get; set; }
public string PostalCode { get; set; }
public string City { get; set; }
}
I want to write attribute [MarkNonNullablePropertiesAsRequired] to mark all fields as required, so swagger can show them as required when project, launched.
I created class for attribute
[AttributeUsage(AttributeTargets.Class)]
public class MarkNonNullablePropertiesAsRequired: Attribute
{
}
How I can do this correctly and what I need to write inside class?

How to suppress based-class property in favor of the same new derived-class property on model creating

I have a base class MyBaseClass, with a wrapper class MyWrapperClass which is derived from that base class. The purpose of declaring wrapper class is to detaching the EntityFramework-related annotations, Id properties and navigation properties from the base class. The structure of base class and derived class are as follows:
public class MyBaseClass
{
public ClassA MyProperty { get; set; }
}
public class MyWrapperClass: MyBaseClass
{
public int Id { get; set; }
public new ClassAWrapper MyProperty { get; set; }
}
I cannot use virtual...override pattern on MyProperty, because of the differet types for MyProperty in base class and derived class.
Thus, i put new keyword before MyProperty property, and i expect the EntityFramework to create the model based on ClassAWrapper instead of ClassA.
But i get an error when trying to add-migration: "ClassA should have a key". While ClassA has not a key property, but ClassAWrapper has.
DbContext implementation is as follows:
public class MyDbContext : DbContext
{
public DbSet<MyDerivedClass> MyDerivedClass { get; set; }
...
}
Problem: I need to tell the EntityFramework to give up on the MyProperty class of the base class (which is of type ClassA), and look at the same property in the derive class (with type ClassAWrapper) when creating the model.
Try NotMapped and Key data annotations, like this:
public class MyBaseClass
{
[NotMapped]
public ClassA MyProperty { get; set; }
}
public class MyWrapperClass: MyBaseClass
{
[Key]
public int Id { get; set; }
public new ClassAWrapper MyProperty { get; set; }
}

Overriding attributes for inherited fields

Assume I have a class:
abstract class MyBaseClass {
[Attribute1]
[Attribute2]
public string Property1 {get; set; }
[Attribute3]
[Attribute4]
public string Property2 {get; set; }
}
In the child class, that extends this class I want to add new attributes to the Property1 and Property2, preserving attributes, declared in the parent class.
Is this possible?
abstract class MyBaseClass {
[Attribute1]
[Attribute2]
public virtual string Property1 {get; set; }
[Attribute3]
[Attribute4]
public virtual string Property2 {get; set; }
}
class NewClass:MyBaseClass
{
[Attribute5]
public override string Property1 {get;set;}
[Attribute6]
public override string Property2 {get;set;}
}
Ensure, that Attributes 1-4 use inherited = true
[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class Attribute1 : Attribute
{
}
Yes, you can do this overriding Property1 and Property2. You obviously need to make them virtual in the base class:
class MyAttribute: Attribute {}
class YourAttribute: Attribute {}
class Base
{
[My]
public virtual void Foo() {}
}
class Derived: Base
{
[Your]
public override void Foo()
{
}
}
And now var attributes = typeof(Derived).GetMethod("Foo").GetCustomAttributes(); will return both MyAttribute and YourAttribute instances of Derived.Foo.
Do note that all GetAttribute type methods have an overload that lets you specify if you want inherited attributed to be included in the result or not. Default behavior is to include inherited attributes.
It is only possible if the attribute that you want to inherit is not specified with AttributeUsageAttribute.Inherited = false. For example, inherting the ObsoleteAttribute does not work:
abstract class MyBaseClass {
[Obsolete]
public virtual string Property1 {get; set; }
}
class Derived : MyBaseClass
{
public override string Property1 {get; set;}
}
In this example, you get the warning message:
Member 'Property1' overrides obsolete member 'Property1'. Add the
Obsolete attribute to 'Property1'
By default, attributes are set with the Inherit = True flag. So if you create a custom attribute, inheritance should work fine.

How can I disable required attribute on a property in a model in web api?

How to disable the [Required] attribute that has been set on a model property.
I tried with below code using new keyword but not working.
I also tried override keyword as well not worked.
ChildModel uses most of the properties of BaseModel that's instead of create new model file and code many similar properties I'm thinking to do something like this.
public class BaseModel
{
[Required]
public string Address{ get; set; }
}
public class ChildModel : BaseModel
{
public new string Address{ get; set; }
}
Any simple solution ?
Simply overriding or redeclaring using the new keyword on the property and removing the attribute does not work. The way I have always done this is like below.:
public abstract class BaseModel
{
public abstract string Address { get; set; }
}
public class ChildModel : BaseModel
{
[Required]
public override string Address { get; set; }
}
public class AnotherChildModel : BaseModel
{
//Not[Required]
public override string Address { get; set; }
}
You can read this thread if you want to know more on how attributes of a base class are treated during inheritance.

How to set order of appearance for fields when using Html.EditorFor in MVC 2?

I have the following classes in my Model:
public abstract class Entity : IEntity
{
[ScaffoldColumn(false)]
public int Id { get; set; }
[Required,StringLength(500)]
public string Name { get; set; }
}
and
public class Model : SortableEntity
{
[Required]
public ModelType Type { get; set; }
[ListRequired]
public List<Producer> Producers { get; set; }
public List<PrintArea> PrintAreas { get; set; }
public List<Color> Colors { get; set; }
}
To display the "Model" class in the view I simply call Html.EditorFor(model=>model), but the "Name" property of the base class is rendered last, which is not the desired behaviour.
Is it possible to influenece on the order of displayed fields somehow?
I've not been able to find an attribute for that, so your options are:
1) create one, and then revise the base Object.ascx template to account for it, or
2) create a custom editor template for your classes that explicitly put stuff in the order you want.

Categories