Asked by:
how do i get the categoryname from fields instead of properties?

Question
-
User-1982615125 posted
This gets the categoryname from the first property in the Customer object
dim customer as new customer
Dim categoryname As String = CType(TypeDescriptor.GetProperties(customer)(0).Attributes(GetType(CategoryAttribute)), CategoryAttribute).Category
my question
how do i get the categoryname from fields instead of properties?
Wednesday, June 20, 2018 2:36 PM
All replies
-
User475983607 posted
This gets the categoryname from the first property in the Customer object
dim customer as new customer
Dim categoryname As String = CType(TypeDescriptor.GetProperties(customer)(0).Attributes(GetType(CategoryAttribute)), CategoryAttribute).Category
my question
how do i get the categoryname from fields instead of properties?
My best guess is you are trying to fetch information stored in an attribute.
Is there anyway you can provide the attribute, class, and expected results?
Wednesday, June 20, 2018 2:44 PM -
User-1982615125 posted
i have already came acros that article.
it doesnt explain how to get the category name from a field. it doesnt cover fields at all.
Wednesday, June 20, 2018 4:24 PM -
User475983607 posted
i have already came acros that article.
it doesnt explain how to get the category name from a field. it doesnt cover fields at all.
Like many of your posts, there is not enough context to understand what you're trying to do. For the second time, post the class source, attribute source, and explain what results you expect.
Wednesday, June 20, 2018 4:29 PM -
User-1982615125 posted
i want to get the Category name of a field.
like: myobject.getfields(0).category <-- extremely simplified fantasy example
The visual studio property window also gives the categories of fields. its the same thing that im trying to do.
Wednesday, June 20, 2018 5:05 PM -
User475983607 posted
i want to get the Category name of a field.
like: myobject.getfields(0).category <-- extremely simplified fantasy example
The visual studio property window also gives the categories of fields. its the same thing that im trying to do.
Maybe this is what you're after.
https://msdn.microsoft.com/en-us/library/system.componentmodel.categoryattribute(v=vs.110).aspx
Wednesday, June 20, 2018 5:14 PM -
User-1982615125 posted
ive been there, but it gets the category of properties. not fields
Wednesday, June 20, 2018 5:17 PM -
User475983607 posted
ive been there, but it gets the category of properties. not fields
What are you calling a field? Is "Appearance" what you're looking for?
[Description("The image associated with the control"),Category("Appearance")] public Image MyImage { get { // Insert code here. return image1; } set { // Insert code here. } }
Wednesday, June 20, 2018 5:20 PM -
User-1982615125 posted
what you sent, was a property.
a field is just a variable in the root of an class or structure. for example, Name is a field of Customer. but creationtime is a property of customer
public Class Customer
Public name as string
public property CreationTime as datetime
End classWednesday, June 20, 2018 5:25 PM -
User-1982615125 posted
as you can see, in VB what you made in C#, is a property
<Description("The image associated with the control"), Category("Appearance")>
Public Property MyImage As Image
Get
Return image1
End Get
Set(ByVal value As Image)
End Set
End PropertyWednesday, June 20, 2018 5:26 PM -
User475983607 posted
fazioliamboina
what you sent, was a property.
a field is just a variable in the root of an class or structure. for example, Name is a field of Customer. but creationtime is a property of customer
public Class Customer
Public name as string
public property CreationTime as datetime
End classI know what a field is but you're questions generally make little no sense and you don't provide code. So I have to ask clarifying questions.
Do you have a class with a field named "categoryname" and you want to use reflection to get a this field?
https://msdn.microsoft.com/en-us/library/4ek9c21e(v=vs.110).aspx
Wednesday, June 20, 2018 5:31 PM -
User-1982615125 posted
no. i have a field. lets name this field MYFIELD.
and i want to get the category name of MYFIELD
output:
The categoryname of MYFIELD is GLOBAL SETTING
<Category("GLOBAL SETTINGS")> Public MYFIELD As String
Wednesday, June 20, 2018 5:33 PM -
User-1982615125 posted
for example, provide me code that extracts the category name 'LAYOUT' from the field 'Width' from a tablecell object.
Wednesday, June 20, 2018 5:34 PM -
User475983607 posted
for example, provide me code that extracts the category name 'LAYOUT' from the field 'Width' from a tablecell object.
What tablecell? I give up. It is abundantly clear from this and previous posts that you are not on the forum for assistance... Good luck.
Wednesday, June 20, 2018 5:38 PM -
User-1982615125 posted
the table cell is just an imaginary context for you to understand what im trying to do. The thing is i dont have a context, i just try to do things that would work in ANY context.
so here is a context to work with: this table cell
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.tablecell(v=vs.110).aspx
it has a property called 'Width' and has been categorised 'Layout'. but forget it, because unfortunately my field is not a property
in the mean time. can you make code that extracts the category name from a field?
Wednesday, June 20, 2018 5:43 PM -
User-1982615125 posted
how to get the category name from a field?
This works with properties | it extracts the category name of the first (0) field in the Customer object
Dim customer as new customer
Dim catstring As String = CType(TypeDescriptor.GetProperties(Customer)(0).Attributes(GetType(CategoryAttribute)), CategoryAttribute).Category
but how to do this with fields instead of properties? | for example, how to extract the category name 'General settings' from this 'Field' field.
Public Class Customer
<Category("general settings")> Public Field As String
end class
Wednesday, June 20, 2018 6:12 PM -
User-1982615125 posted
this does the trick, but i find it kinda hacky
dim customer as new customer
MsgBox(Split(Split(Customer.GetType.GetFields.First.CustomAttributes.Where(Function(c) c.AttributeType = GetType(CategoryAttribute)).First.ToString, """", 2)(1), """", 2)(0))
it exports a CustomattributeData object to a string, and then i just fish the category name out of that string using splitting.
but is there a safer/more straightforward way to do this?
Wednesday, June 20, 2018 6:25 PM -
User283571144 posted
Hi fazioliamboina,
According to your description, I suggest you could consider using the System.Reflection's FieldInfo to achive your requirement,
The FieldInfo class contains the information for the fields and we could get the attribute from it.
More details, you could refer to below codes:
Dim props As FieldInfo() = GetType(Customer).GetFields() For Each prop As FieldInfo In props Dim attrs As Object() = prop.GetCustomAttributes(True) For Each attr As Object In attrs Dim authAttr As CategoryAttribute = TryCast(attr, CategoryAttribute) If authAttr IsNot Nothing Then Dim propName As String = prop.Name Dim auth As String = authAttr.Category End If Next Next
Result:
Best Regards,
Brando
Thursday, June 21, 2018 7:57 AM