i saw in some code that use Description attribute for class property and i couldn't find reason of behavior in c# codding
public class sample
{
[Description("description1")]
public string PropertyOnne{ get; set; }
}
for readability of code we can use xml summary for even property and i didn't understand what is difference between summary and Description attribute in class level.
Simple words, you can consider below explanation
The tag is used to generate documentation in XML for your Project at Compile time, this is also used by the visual studio for its intellisense database
The Description attribute used by the designer in order to understand the text, mostly at the bottom of the property window(for reference).
This is for visual designers, they can display the description when referencing them.
Remark from the docs:
A visual designer can display the specified description when referencing the component member, such as in a Properties window. Call Description to access the value of this attribute.
Source: https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.descriptionattribute?view=netframework-4.7.2
Related
I have already searched here to answer my question, and the closest I ever got was that post, but it still does not completely clarify the matter to me, so I will ask.
What I need is extending maxlengthattribute in the way that when I set it inside the C# file,
[MaxLength(50)]
[Display(Name = "Project Description")]
public string ProjectDescription { get; set; }
the attribute "maxlength" will be added inside the tag and will be <\stuff stuff maxlength = "50">
I have initially implemented it as writing html-helper to modify TextBoxFor, however, this is out of option since project is tightly intertwined with .js, and inserting renamed method will break a code, which will be a pain to fix.
The answer I referred above provides the closest explanation of what I need, but it looks like I will need to declare attributes (like inside ( ) in function), which I do not see there.
At this point I can either try modifying JS file on the server-side, or extending maxlengthattribute. Thus far, latter is preferable for me, thus I would like to ask how to properly extend it inside the c# file.
Thank you very much in advance!
You can write a custom dataannotation provider. The max length attribute will get added to the control.
Refer this link
I have a edmx model created from a database and a metadata.cs for it.
In the client, the .g.cs includes [StringLength(X)] attributes alongside my attributes from my metadata.
I am doing some serverside validation for a flat file import that is seperate to the client side editors of these entities.
I am able to apply my range and regular expression validations but I am unable to find the StringLength attribute on the server. Does anyone know how to do this without duplicating the StringLength attributes manually on the metadata properties.
Edit:
Here is some code:
Server side file ProductService.metadata.cs:
internal sealed class PRODUCTMetadata
{
[Required]
[RegularExpression("[A-Z]+")]
[Display(Name = "Product Code", Order = 10)]
public string Product_code { get; set; }
}
Client side Generated_Code\NameSpace.Web.g.cs:
public sealed class PRODUCT
{
[DataMember()]
[Display(Name="Product Code", Order=10)]
[RegularExpression("[A-Z]+")]
[Required()]
[StringLength(8)] //This is what I want to know, but server side
public string Product_code
{...etc
}
}
I've investigated a bit around this problem, and couldn't find any good information about the topic on the Internet. So what' I'll say here is only assumption.
As you have seen, the auto-generated client proxy code is much more decorated with attributes than the server-side code. Your entities for instance have the nice [StringLength(8)] attribute that comes from the Entity Model. On the server side, the auto-generated .metadata.cs file doesn't have those attributes on entities. I think it's all about code-generation templates.
I suspect that the code generation template of RIA Services (that creates the .g.cs file) is much more complete than the template that creates the .metadata.cs file on server side.
The fact that the attribute that is missing in your case is 95% of time used for UI validation on client-side might explain why the template for the .metadata.cs file doesn't produce those validation attributes.
I see 2 work-arounds for your problem:
1. Write your own metadata class on server side
Some example:
[MetadataTypeAttribute(typeof(PRODUCT.PRODUCTMetadata))]
public partial class PRODUCT
{
internal sealed class PRODUCTMetadata
{
// Metadata classes are not meant to be instantiated.
private PRODUCTMetadata()
{
}
[StringLength(8)]
public string Product_code { get; set; }
}
}
You can manually add any attributes to the properties of your entities, as entities are partial classes.
Unfortunately, you'll have to maintain those metadatas each time you modify your model: if (for example), your DB table column changes from varchar(8) to varchar(10), you'll be able to automatically update your EDMX model from your database, but you'll have to manually check that your metadatas are still OK (in this example, you would have to manually replace [StringLength(8)] by [StringLength(9)]).
Here's a nice link about metadata.
2. Modify the T4 templates
Second option is probably the best one, but I didn't experienced myself the code generation template modification, so I don't known what can effectively be done or not.
Code generation templates are known as T4 templates (Text Template Transformation Toolkit). It is possible to modify those templates to include anything you want in the code generation process. You could modify the default EF template so it generates the missing attributes just as the RIA Services template does.
Here's some nice articles about T4 code generation:
http://www.scip.be/index.php?Page=ArticlesNET36#T4CodeGenerationClasses
http://msdn.microsoft.com/en-us/data/gg558520
http://msdn.microsoft.com/en-us/library/cc982041.aspx
I write this as an answer (it wouldn't fit as a comment), but remember it's all assumptions.
I have been using mvc2 for a while now, and when i need to set the template i use the DataType Attribute
[DataType("DropDown")]
public int Field { get; set; }
I see others using UiHint to achieve the same results
[UiHint("DropDown")]
public int Field { get; set; }
What is the difference between using these two attributes? Which attribute should I be normally using, or are they for different tasks?
DataType is generally used to make it known that this is a very specific version of a property, such as price.
The most common example of DataType is the [DataType(DataTypes.EmailAddress)] which usually is a string but we're saying that this is a very specific type of string.
They're both helpful and the UIHint overrides the DataType. So if you have a certain DataType but you want to override the editor for that specific property you can use a UIHint.
DataType attribute has two purposes
Provide additional type information for a data field. You do this by applying the DataTypeAttribute attribute to a data field in the data model and by specifying the additional type name from the DataType enumeration. Then the view engine uses the default template for displaying the property, like, a checkbox for a boolean.
If you want to override the default template, and wish to use a custom template, then it can be used to associate a custom field template with that data field. In this case you must provide a partial page[.cshtml, MVC 4] to describe the display.
The purpose of UIHint is exactly same as the second point above.
Where to use what? The answer is: context, ie., what will make more sense, what is closer to the physical problem your code is trying to solve.
What if both are applied to the same property? The answer is: UIHint has precedence, obviously. But why would you apply both?
I got below code from http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx for adding custom property in webpart tool pane. What does square bracket ([]) mean in the below code?
[Category("Custom Properties")]
[WebPartStorage(Storage.Personal)]
[FriendlyNameAttribute("Custom Color")]
[Description("Select a color from the dropdown list.")]
[Browsable(true)]
[XmlElement(typeof(System.Drawing.KnownColor))]
public System.Drawing.KnownColor MyColor
{
get
{
return _myColor;
}
set
{
_myColor = value;
}
}
As #Spencer Ruport said, they're attributes. They're used within .NET for declarative programming.
You can find information on each of these attributes at MSDN. However, you should know that the name of the attribute can be shortened. In your case, for example, Category is the short form of the class name CategoryAttribute and XmlElement is the short form of the class name XmlElementAttribute. When declaring attributes, the Attribute portion of the class name can be left out.
I've used most of these attributes in conjunction with the PropertyGrid control (see here for an example), although in your case, they are used for a Web Part property pane. The purpose is still the same. The attributes are used by the control to know how to display the property to the user. By using a combination of the various attributes that the control understands, it is possible to declaratively dictate this behavior.
I hope that helps a little bit, but Spencer is correct, you'll learn a lot more reading about attributes via Google than I can explain here.
They're called attributes.
Here's a quick example of how they can be used: http://www.codeproject.com/KB/cs/attributes.aspx
Using the standard VS IDE, is there a fast way to create class properties that are linked to the local variables?
The class diagram seems to provide something, but it basically just created a property stub. Is there something better / easier out there ?
In VS.NET 2008 you can use refactoring, Encapsulate field (ctrl + r, e).
Here is info about how Refactoring In Visual Studio 2008
If you're talking about just making quick properties, then Auto-Generated properties are 'the bomb'. There's no need for a background local variable unless you plan to do something special in the get or set.
public string SampleProperty { get; set; }
or
public string SampleProperty { get; private set; }
Where you can optionally specify private / protected to limit the property to a setter or getter only. Then, you don't need a local variable and you just use the Property in place of the local variable. The compiler will generate the actual background variable for you.
I think you might be confusing an Auto Generated property with a property stub.
When building classes in Visual Studio (VS), you can generate property setters and getters quickly by defining a field variable, and then right-clicking on the field and selecting Refactor → Encapsulate Field from the popup menu. VS will display a dialog that lets you approve/change the property name, and optionally, preview changes. When you're satisfied, simply click OK. Voilà! VS generates the property!