473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How do I change the Category in property grid for dynamic types?

I want to use the property grid to display instances of dynamic types. A
problem I'm having is that the property grid is not respecting attributes
that are set with SetCustomAttribute. I've taken an example from MSDN and
added a few lines to set the CateoryAttribute of my property. Unfortunetly
it still comes up as misc. What's wrong with this sample?

public static Assembly DefineDynamicAssembly(AppDomain domain)
{
// Build a dynamic assembly using Reflection Emit API.

AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "MyDynamicAssembly";

AssemblyBuilder assemblyBuilder =
domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder =
assemblyBuilder.DefineDynamicModule("MyDynamicModu le");
typeBuilder = moduleBuilder.DefineType("MyDynamicType",
TypeAttributes.Public);
FieldBuilder customerNameBldr = typeBuilder.DefineField("customerName",
typeof(string),
FieldAttributes.Private);
PropertyBuilder custNamePropBldr =
typeBuilder.DefineProperty("CustomerName",
PropertyAttributes.RTSpecialName |PropertyAttributes.SpecialName,
typeof(string),
new Type[] { typeof(string) });
Type attType = typeof(System.ComponentModel.CategoryAttribute);
ConstructorInfo constructorInfo = attType.GetConstructor(new
Type[]{typeof(string)});
CustomAttributeBuilder attributeBuilder = new
CustomAttributeBuilder(constructorInfo, new object[]{"Category"});
custNamePropBldr.SetCustomAttribute(attributeBuild er);
// First, we'll define the behavior of the "get" property for
CustomerName as a method.
MethodBuilder custNameGetPropMthdBldr =
typeBuilder.DefineMethod("GetCustomerName",
MethodAttributes.Public,
typeof(string),
new Type[] { });

ILGenerator custNameGetIL = custNameGetPropMthdBldr.GetILGenerator();

custNameGetIL.Emit(OpCodes.Ldarg_0);
custNameGetIL.Emit(OpCodes.Ldfld, customerNameBldr);
custNameGetIL.Emit(OpCodes.Ret);

// Now, we'll define the behavior of the "set" property for CustomerName.
MethodBuilder custNameSetPropMthdBldr =
typeBuilder.DefineMethod("SetCustomerName",
MethodAttributes.Public,
null,
new Type[] { typeof(string) });

ILGenerator custNameSetIL = custNameSetPropMthdBldr.GetILGenerator();

custNameSetIL.Emit(OpCodes.Ldarg_0);
custNameSetIL.Emit(OpCodes.Ldarg_1);
custNameSetIL.Emit(OpCodes.Stfld, customerNameBldr);
custNameSetIL.Emit(OpCodes.Ret);

// Last, we must map the two methods created above to our PropertyBuilder
to
// their corresponding behaviors, "get" and "set" respectively.
custNamePropBldr.SetGetMethod(custNameGetPropMthdB ldr);
custNamePropBldr.SetSetMethod(custNameSetPropMthdB ldr);


ConstructorBuilder constructorBuilder =
typeBuilder.DefineConstructor(MethodAttributes.Pub lic,
CallingConventions.Standard, null);
ILGenerator ilGenerator = constructorBuilder.GetILGenerator();

ilGenerator.EmitWriteLine("MyDynamicType instantiated!");
ilGenerator.Emit(OpCodes.Ret);

typeBuilder.CreateType();
return assemblyBuilder;
}
}

Jul 21 '05 #1
0 1556

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Robert Hooker | last post by:
Hi all, I'm looking for a way to sort/order the Category text items in a property grid. The grids sort the categories by alpha when the catergories are displayed. Does anyone at Microsoft (or...
5
by: Polo | last post by:
Hi All, I have defined some properties on a object with a category Attribute How can I use a custom sort inside a category ? See below By default : General information <- Category
3
by: Andrew Dodgshun | last post by:
I have 2 grids - one shows a list of table names in a database and when you click on a table name the other grid dynamically populates the grid with the table contents. My problem is that I cannot...
0
by: SlipperyCat | last post by:
I want to use the property grid to display instances of dynamic types. A problem I'm having is that the property grid is not respecting attributes that are set with SetCustomAttribute. I've taken...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
6
by: Ron M. Newman | last post by:
Hi, I have a problem with dynamically putting dynamic properties on the property grid. My app has no idea what it'll be showing at compile time. How do I dynamically add properties to the...
4
by: Jo Claes | last post by:
Hi, A friend asked me if i could help him building a lilttle website. Having 10 years of proffessional experiance in writing "non-webbased" code i thought that will not be that hard. I think...
2
by: jbaldi | last post by:
I am trying to use a collection editor to edit multiple types as in the following example: ...
0
by: Czechtim | last post by:
Hello, I have problem with databinding. I created small application using structure that I need to demonstrate problem. I need to change content of label when changing content of property...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.