Answered Custom Types and ICustomPropertyProvider

  • Saturday, April 28, 2012 12:59 PM
     
      Has Code

    I'm trying to create Types on the fly and bind them via ICustomPropertyProvider, and the binding failed shortly after ICustomPropertyProvider.Type(), is there something wrong with the way I create the Type? ICustomPropertyProvider works when I declare property CustomerName on Class1, but not with dynamically-generated type.

    public Class1: ICustomPropertyProvider { public static void Init() { AssemblyName assemblyName = new AssemblyName("Sample"); AssemblyBuilder asm = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); ModuleBuilder myModBuilder = asm.DefineDynamicModule("Module1"); TypeBuilder myTypeBuilder = myModBuilder.DefineType("CustomerData", TypeAttributes.Public); FieldBuilder customerNameBldr = myTypeBuilder.DefineField("customerName", typeof(string), FieldAttributes.Private); // The last argument of DefineProperty is null, because the // property has no parameters. (If you don't specify null, you must // specify an array of Type objects. For a parameterless property, // use an array with no elements: new Type[] {}) PropertyBuilder custNamePropBldr = myTypeBuilder.DefineProperty("CustomerName", PropertyAttributes.HasDefault, typeof(string), null); // The property set and property get methods require a special // set of attributes. MethodAttributes getSetAttr = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; // Define the "get" accessor method for CustomerName. MethodBuilder custNameGetPropMthdBldr = myTypeBuilder.DefineMethod("get_CustomerName", getSetAttr, typeof(string), new Type[] {}); ILGenerator custNameGetIL = custNameGetPropMthdBldr.GetILGenerator(); custNameGetIL.Emit(OpCodes.Ldarg_0); custNameGetIL.Emit(OpCodes.Ldfld, customerNameBldr); custNameGetIL.Emit(OpCodes.Ret); // Define the "set" accessor method for CustomerName. MethodBuilder custNameSetPropMthdBldr = myTypeBuilder.DefineMethod("set_CustomerName", getSetAttr, 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(custNameGetPropMthdBldr); custNamePropBldr.SetSetMethod(custNameSetPropMthdBldr); //_typ= myTypeBuilder.CreateType(); _type = myTypeBuilder.MakeByRefType(); _property = new CustomPropertyBase(_type, "CustomerName"); }

            ICustomProperty ICustomPropertyProvider.GetCustomProperty(string name)
            {
                return _property;
            }

            Type ICustomPropertyProvider.Type
            {
                get { return _type; }
            }

    } public class CustomPropertyBase : ICustomProperty { string _Name = null; Type _type = null; public CustomPropertyBase(Type type, string Name) { _type = type; _Name = Name; } bool ICustomProperty.CanRead { get { return true; } } bool ICustomProperty.CanWrite { get { return true; } } object ICustomProperty.GetIndexedValue(object target, object index) { throw new NotImplementedException(); } object ICustomProperty.GetValue(object target) { return "Greenwich"; } string ICustomProperty.Name { get { return _Name; } } void ICustomProperty.SetIndexedValue(object target, object value, object index) { throw new NotImplementedException(); } void ICustomProperty.SetValue(object target, object value) { throw new NotImplementedException(); } Type ICustomProperty.Type { get { return _type; } } }




    • Edited by teyc Saturday, April 28, 2012 12:59 PM
    • Edited by teyc Saturday, April 28, 2012 1:01 PM
    •  

All Replies

  • Tuesday, May 01, 2012 3:58 PM
    Moderator
     
     Answered
    Do you have XAML to go along with this?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator

    • Marked As Answer by teyc Wednesday, May 02, 2012 12:06 AM
    •  
  • Wednesday, May 02, 2012 12:06 AM
     
     Answered Has Code

    Thanks Matt. My error was because I wrongly thought I needed to create a dynamic type to go with a ICustomPropertyProvider. In fact, if I did the following, and not built types on the fly:

    Type ICustomPropertyProvider.Type        
    {            
      get { 
        return typeof(Class1)
        //return _type; 
      }        
    }

     ICustomPropertyProvider works as advertised.