473,320 Members | 2,048 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,320 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 1552

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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.