0

I'm trying to create custom tokens for more than one facet field. GetMyCustomFacetValue (this MyCustomFacet facet) is my function to return the responding value to the current token for examlpe ($firstname, $lastname). I need to get the current token to decide which facet field value to return

namespace FooBar
{
    public class CustomRecipientPropertyTokenMap : DefaultRecipientPropertyTokenMap
    {
        protected static readonly MethodInfo GetMyFacetValue = typeof(FacetExtensions).GetMethod(nameof(FacetExtensions.GetMyCustomFacetValue), new[] { typeof(MyCustomFacet) });

        static CustomRecipientPropertyTokenMap()
        {
            if (TokenBindings == null)
            {
                TokenBindings = new Dictionary<Token, RecipientPropertyTokenBinding>();
            }

            RecipientPropertyTokenBinding customTokenBinding = RecipientPropertyTokenBinding.Build<MyCustomFacet>(new Token("customtokenkey"), null, GetMyFacetValue);
            TokenBindings.Add(customTokenBinding.Token, customTokenBinding);
        }
    }

    public static class FacetExtensions
    {
        public static string GetMyCustomFacetValue (this MyCustomFacet facet)
        {
         //I need the current token that calls this function
        }
    }
}
0

You need to create a separate extension method for each custom token that you have. The GetMyCustomFacetValue method doesn't receive a currentToken parameter, that's why it cannot cater for all of the custom tokens that are related to the MyCustomFacet.

So, what you should be doing instead is something like:

public static class FacetExtensions
    {
        public static string GetFoo (this MyCustomFacet facet)
        {
          return facet.Foo;
        }

        public static string GetBar (this MyCustomFacet facet)
        {
          return facet.Bar;
        }
    }
}

Another option is to use pass a Func<MyCustomFacet, object> to the RecipientPropertyTokenBinding.Build method, this will save you couple of lines of code.

RecipientPropertyTokenBinding.Build<MyCustomFacet>(new Token("foo"), myCustomFacet => myCustomFacet.Foo, null),
RecipientPropertyTokenBinding.Build<MyCustomFacet>(new Token("bar"), myCustomFacet => myCustomFacet.Bar, null),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.