Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6934249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:56:08+00:00 2026-05-27T11:56:08+00:00

On my Silverlight client, I’m generating a class at run-time to bind it to

  • 0

On my Silverlight client, I’m generating a class at run-time to bind it to a datagrid. I’m using a method based on this blog post.
Now I want to use datagrid cellvalidation by calling ValidateProperty in the property setter. But as the properties are generated at runtime, I need to do this in Reflection.Emit.

This is the C# I want to generate in IL:

    public int TestProperty
    {
        get { return testProperty; }
        set 
        {
            Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "TestProperty" });
            testProperty = value; 
        }
    }

This is how ILspy decompiles this method in IL:

.property int32 TestProperty
{
    .get public hidebysig specialname 
        instance int32 get_TestProperty () cil managed 
    {
        // Method begins at RVA 0x224c
        // Code size 12 (0xc)
        .maxstack 1
        .locals init (
            [0] int32 
        )

        IL_0000: nop
        IL_0001: ldarg.0
        IL_0002: ldfld int32 class SilverlightApplication2.testclass::testProperty
        IL_0007: stloc.0
        IL_0008: br.s IL_000a
        IL_000a: ldloc.0
        IL_000b: ret
    } // End of method testclass.get_TestProperty
    .set public hidebysig specialname 
        instance void set_TestProperty (
            int32 value
        ) cil managed 
    {
        // Method begins at RVA 0x2264
        // Code size 43 (0x2b)
        .maxstack 5
        .locals init (
            [0] class [System.ComponentModel.DataAnnotations]System.ComponentModel.DataAnnotations.ValidationContext 
        )

        IL_0000: nop
        IL_0001: ldarg.1
        IL_0002: box int32
        IL_0007: ldarg.0
        IL_0008: ldnull
        IL_0009: ldnull
        IL_000a: newobj instance void [System.ComponentModel.DataAnnotations]System.ComponentModel.DataAnnotations.ValidationContext::.ctor(object, class [mscorlib]System.IServiceProvider, class [mscorlib]System.Collections.Generic.IDictionary`2<object, object>)
        IL_000f: stloc.0
        IL_0010: ldloc.0
        IL_0011: ldstr "TestProperty"
        IL_0016: callvirt instance void [System.ComponentModel.DataAnnotations]System.ComponentModel.DataAnnotations.ValidationContext::set_MemberName(string)
        IL_001b: nop
        IL_001c: ldloc.0
        IL_001d: call void [System.ComponentModel.DataAnnotations]System.ComponentModel.DataAnnotations.Validator::ValidateProperty(object, class [System.ComponentModel.DataAnnotations]System.ComponentModel.DataAnnotations.ValidationContext)
        IL_0022: nop
        IL_0023: ldarg.0
        IL_0024: ldarg.1
        IL_0025: stfld int32 class SilverlightApplication2.testclass::testProperty
        IL_002a: ret
    } // End of method testclass.set_TestProperty
}

And this is me trying to write it in Reflection.Emit:

        setIL.Emit(OpCodes.Ldarg_1);
        setIL.Emit(OpCodes.Box, typeof(Int32));
        setIL.Emit(OpCodes.Ldarg_0);
        setIL.Emit(OpCodes.Ldnull);
        setIL.Emit(OpCodes.Ldnull);

        Type[] types = new Type[3];
        types[0] = typeof(object);
        types[1] = typeof(IServiceProvider);
        types[2] = typeof(IDictionary<object, object>);

        setIL.Emit(OpCodes.Newobj, typeof(System.ComponentModel.DataAnnotations.ValidationContext).GetConstructor(types));
        setIL.Emit(OpCodes.Stloc_0);
        setIL.Emit(OpCodes.Ldloc_0);
        setIL.Emit(OpCodes.Ldstr, "TestProperty");
        setIL.Emit(OpCodes.Callvirt, typeof(System.ComponentModel.DataAnnotations.ValidationContext).GetMethod("set_MemberName"));
        setIL.Emit(OpCodes.Ldloc_0);
        setIL.Emit(OpCodes.Call, typeof(System.ComponentModel.DataAnnotations.Validator).GetMethod("ValidateProperty"));

        setIL.Emit(OpCodes.Ldarg_0);
        setIL.Emit(OpCodes.Ldarg_1);
        setIL.Emit(OpCodes.Stfld, fieldBuilder);
        setIL.Emit(OpCodes.Ret);

This is only a part of my code, I managed to get it working without the Validator.ValidateProperty with the last 4 lines of code. With the 16 other lines I want to add the validate functionality, but right now this results in a ‘Operation could destabilize the runtime’ exception.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:56:09+00:00Added an answer on May 27, 2026 at 11:56 am

    Figured it out myself 🙂

    This is the code you need to emit Validator.Validate in your property setter:

            MethodBuilder setPropMthdBldr =
                tb.DefineMethod("set_" + "TestProperty",
                  MethodAttributes.Public |
                  MethodAttributes.SpecialName |
                  MethodAttributes.HideBySig,
                  null, new Type[] { propertyType });
    
    
            ConstructorInfo ctor1 = typeof(ValidationContext).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                                             null,
                                                                             new Type[]{
                                                                                    typeof(Object),
                                                                                    typeof(IServiceProvider),
                                                                                    typeof(IDictionary<object, object>)},
                                                                             null);
    
            MethodInfo method2 = typeof(ValidationContext).GetMethod("set_MemberName",
                                                                     BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
                                                                     null,
                                                                     new Type[]{typeof(String)},
                                                                     null);
    
            MethodInfo method3 = typeof(Validator).GetMethod("ValidateProperty",
                                                             BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic,
                                                             null,
                                                             new Type[]{
                                                                    typeof(Object),
                                                                    typeof(ValidationContext)},
                                                             null);
    
            ILGenerator setIL = setPropMthdBldr.GetILGenerator();
    
            setIL.DeclareLocal(typeof(System.ComponentModel.DataAnnotations.ValidationContext));
    
            setIL.Emit(OpCodes.Nop);
            setIL.Emit(OpCodes.Ldarg_1);
            setIL.Emit(OpCodes.Box, typeof(Int32));  //in this case it's int32, should be your property type
            setIL.Emit(OpCodes.Ldarg_0);
            setIL.Emit(OpCodes.Ldnull);
            setIL.Emit(OpCodes.Ldnull);
            setIL.Emit(OpCodes.Newobj, ctor1);
            setIL.Emit(OpCodes.Stloc_0);
            setIL.Emit(OpCodes.Ldloc_0);
            setIL.Emit(OpCodes.Ldstr, "TestProperty");
            setIL.Emit(OpCodes.Callvirt, method2);
            setIL.Emit(OpCodes.Nop);
            setIL.Emit(OpCodes.Ldloc_0);
            setIL.Emit(OpCodes.Call, method3);
            setIL.Emit(OpCodes.Nop);
            setIL.Emit(OpCodes.Ldarg_0);
            setIL.Emit(OpCodes.Ldarg_1);
            setIL.Emit(OpCodes.Stfld, fieldBuilder);  //the fieldbuilder you are using to define the private field
            setIL.Emit(OpCodes.Ret);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this setup that works perfectly when using http. A silverlight 3 client
I am using a WCF service and a Silverlight Client sending data to the
(Using Visual Studio 2008, and Silverlight 3) -I've really looked around for this one,
I have written a simplified Silverlight client library for my WCF web service using
I have a silverlight client that can call a method on my WCF web
I'm using .NET RIA Services July Preview to communicate between my Silverlight client and
Imagine this structure. A solution with the following projects /Backend (RIA Silverlight client) /Backend.Web
My environment: Windows Server 2008, IIE 7.0, ASP.NET I developed a Silverlight client. This
I've extended my domain class on the silverlight client side in a partial class.
Okay, so I have this Silverlight client program. I'm not allowed to use the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.