IMarkupExtension - Check property type during compilation time (xamarin.forms)











up vote
0
down vote

favorite












I would like to throw an exception during compilation time if the given parameter in the IMarkupExtension is not compatible with the type expected by me.
Can I achieve this effect?



Below I put my experiments, but I do not know where and how to check what I wrote in "TODO"



Code (I marked todo)



using System;
using Xamarin.Forms.Xaml;

namespace MySample
{
public class SampleClass : IMarkupExtension
{
public IParameter Parameter { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
}
}

public interface IParameter
{
string GetData();
}
public class SampleData1 : IParameter
{
public string GetData()
{
return "Data1";
}
}
public class SampleData2 : IParameter
{
public string GetData()
{
return "Data2";
}
}
}


XAML



<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mysample="clr-namespace:MySample"
x:Class="MySample.SamplePage">
<ContentPage.Resources>
<mysample:SampleData2 x:Key="SampleData2" />
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<Label>
<Label.Text>
<mysample:SampleClass Parameter="{StaticResource SampleData2}" />
</Label.Text>
</Label>
</StackLayout>
</ContentPage.Content>
</ContentPage>


Please note that the parameter is of the SampleData2 type, but I want to throw an exception if it is not of the SampleData1 type.



Resource



<mysample:SampleData2 x:Key="SampleData2" />


Resource usage



Parameter="{StaticResource SampleData2}"


Check (not necessarily in this place, but definitely during compilation)



public object ProvideValue(IServiceProvider serviceProvider)
{
return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
}









share|improve this question


























    up vote
    0
    down vote

    favorite












    I would like to throw an exception during compilation time if the given parameter in the IMarkupExtension is not compatible with the type expected by me.
    Can I achieve this effect?



    Below I put my experiments, but I do not know where and how to check what I wrote in "TODO"



    Code (I marked todo)



    using System;
    using Xamarin.Forms.Xaml;

    namespace MySample
    {
    public class SampleClass : IMarkupExtension
    {
    public IParameter Parameter { get; set; }
    public object ProvideValue(IServiceProvider serviceProvider)
    {
    return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
    }
    }

    public interface IParameter
    {
    string GetData();
    }
    public class SampleData1 : IParameter
    {
    public string GetData()
    {
    return "Data1";
    }
    }
    public class SampleData2 : IParameter
    {
    public string GetData()
    {
    return "Data2";
    }
    }
    }


    XAML



    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:mysample="clr-namespace:MySample"
    x:Class="MySample.SamplePage">
    <ContentPage.Resources>
    <mysample:SampleData2 x:Key="SampleData2" />
    </ContentPage.Resources>
    <ContentPage.Content>
    <StackLayout>
    <Label>
    <Label.Text>
    <mysample:SampleClass Parameter="{StaticResource SampleData2}" />
    </Label.Text>
    </Label>
    </StackLayout>
    </ContentPage.Content>
    </ContentPage>


    Please note that the parameter is of the SampleData2 type, but I want to throw an exception if it is not of the SampleData1 type.



    Resource



    <mysample:SampleData2 x:Key="SampleData2" />


    Resource usage



    Parameter="{StaticResource SampleData2}"


    Check (not necessarily in this place, but definitely during compilation)



    public object ProvideValue(IServiceProvider serviceProvider)
    {
    return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
    }









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I would like to throw an exception during compilation time if the given parameter in the IMarkupExtension is not compatible with the type expected by me.
      Can I achieve this effect?



      Below I put my experiments, but I do not know where and how to check what I wrote in "TODO"



      Code (I marked todo)



      using System;
      using Xamarin.Forms.Xaml;

      namespace MySample
      {
      public class SampleClass : IMarkupExtension
      {
      public IParameter Parameter { get; set; }
      public object ProvideValue(IServiceProvider serviceProvider)
      {
      return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
      }
      }

      public interface IParameter
      {
      string GetData();
      }
      public class SampleData1 : IParameter
      {
      public string GetData()
      {
      return "Data1";
      }
      }
      public class SampleData2 : IParameter
      {
      public string GetData()
      {
      return "Data2";
      }
      }
      }


      XAML



      <?xml version="1.0" encoding="utf-8" ?>
      <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
      xmlns:mysample="clr-namespace:MySample"
      x:Class="MySample.SamplePage">
      <ContentPage.Resources>
      <mysample:SampleData2 x:Key="SampleData2" />
      </ContentPage.Resources>
      <ContentPage.Content>
      <StackLayout>
      <Label>
      <Label.Text>
      <mysample:SampleClass Parameter="{StaticResource SampleData2}" />
      </Label.Text>
      </Label>
      </StackLayout>
      </ContentPage.Content>
      </ContentPage>


      Please note that the parameter is of the SampleData2 type, but I want to throw an exception if it is not of the SampleData1 type.



      Resource



      <mysample:SampleData2 x:Key="SampleData2" />


      Resource usage



      Parameter="{StaticResource SampleData2}"


      Check (not necessarily in this place, but definitely during compilation)



      public object ProvideValue(IServiceProvider serviceProvider)
      {
      return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
      }









      share|improve this question













      I would like to throw an exception during compilation time if the given parameter in the IMarkupExtension is not compatible with the type expected by me.
      Can I achieve this effect?



      Below I put my experiments, but I do not know where and how to check what I wrote in "TODO"



      Code (I marked todo)



      using System;
      using Xamarin.Forms.Xaml;

      namespace MySample
      {
      public class SampleClass : IMarkupExtension
      {
      public IParameter Parameter { get; set; }
      public object ProvideValue(IServiceProvider serviceProvider)
      {
      return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
      }
      }

      public interface IParameter
      {
      string GetData();
      }
      public class SampleData1 : IParameter
      {
      public string GetData()
      {
      return "Data1";
      }
      }
      public class SampleData2 : IParameter
      {
      public string GetData()
      {
      return "Data2";
      }
      }
      }


      XAML



      <?xml version="1.0" encoding="utf-8" ?>
      <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
      xmlns:mysample="clr-namespace:MySample"
      x:Class="MySample.SamplePage">
      <ContentPage.Resources>
      <mysample:SampleData2 x:Key="SampleData2" />
      </ContentPage.Resources>
      <ContentPage.Content>
      <StackLayout>
      <Label>
      <Label.Text>
      <mysample:SampleClass Parameter="{StaticResource SampleData2}" />
      </Label.Text>
      </Label>
      </StackLayout>
      </ContentPage.Content>
      </ContentPage>


      Please note that the parameter is of the SampleData2 type, but I want to throw an exception if it is not of the SampleData1 type.



      Resource



      <mysample:SampleData2 x:Key="SampleData2" />


      Resource usage



      Parameter="{StaticResource SampleData2}"


      Check (not necessarily in this place, but definitely during compilation)



      public object ProvideValue(IServiceProvider serviceProvider)
      {
      return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
      }






      xamarin.forms compilation-time






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 days ago









      g_m

      234




      234
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I think it is not possible to throw exception at compile time. The logical errors cannot be detected by the compiler, and hence they are detected only when the program is executed.



          Compile time error:



          If we do not follow the proper syntax and semantics of any programming language then the compiler throw compile time error.



          For example:



          1.Missing semicolon



          2.writing keywords in uppercase



          3.varaiable not defiend etc



          Runtime error:



          A runtime error is generated when the program is in running state. They are often termed as an exception.



          For example:



          1.Division by zero



          2.Running out of memory



          3.Dereferencing null pointer etc



          You can use code below to throw a Exception when this function triggered and Parameter is not of the SampleData1 type.



           public object ProvideValue()
          {

          if (Parameter is SampleData1)
          {

          return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
          }
          else if (Parameter is SampleData2)
          {

          throw new Exception("Parameter must be of type SampleData1");
          }
          return "error";
          }





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343545%2fimarkupextension-check-property-type-during-compilation-time-xamarin-forms%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I think it is not possible to throw exception at compile time. The logical errors cannot be detected by the compiler, and hence they are detected only when the program is executed.



            Compile time error:



            If we do not follow the proper syntax and semantics of any programming language then the compiler throw compile time error.



            For example:



            1.Missing semicolon



            2.writing keywords in uppercase



            3.varaiable not defiend etc



            Runtime error:



            A runtime error is generated when the program is in running state. They are often termed as an exception.



            For example:



            1.Division by zero



            2.Running out of memory



            3.Dereferencing null pointer etc



            You can use code below to throw a Exception when this function triggered and Parameter is not of the SampleData1 type.



             public object ProvideValue()
            {

            if (Parameter is SampleData1)
            {

            return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
            }
            else if (Parameter is SampleData2)
            {

            throw new Exception("Parameter must be of type SampleData1");
            }
            return "error";
            }





            share|improve this answer

























              up vote
              0
              down vote













              I think it is not possible to throw exception at compile time. The logical errors cannot be detected by the compiler, and hence they are detected only when the program is executed.



              Compile time error:



              If we do not follow the proper syntax and semantics of any programming language then the compiler throw compile time error.



              For example:



              1.Missing semicolon



              2.writing keywords in uppercase



              3.varaiable not defiend etc



              Runtime error:



              A runtime error is generated when the program is in running state. They are often termed as an exception.



              For example:



              1.Division by zero



              2.Running out of memory



              3.Dereferencing null pointer etc



              You can use code below to throw a Exception when this function triggered and Parameter is not of the SampleData1 type.



               public object ProvideValue()
              {

              if (Parameter is SampleData1)
              {

              return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
              }
              else if (Parameter is SampleData2)
              {

              throw new Exception("Parameter must be of type SampleData1");
              }
              return "error";
              }





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I think it is not possible to throw exception at compile time. The logical errors cannot be detected by the compiler, and hence they are detected only when the program is executed.



                Compile time error:



                If we do not follow the proper syntax and semantics of any programming language then the compiler throw compile time error.



                For example:



                1.Missing semicolon



                2.writing keywords in uppercase



                3.varaiable not defiend etc



                Runtime error:



                A runtime error is generated when the program is in running state. They are often termed as an exception.



                For example:



                1.Division by zero



                2.Running out of memory



                3.Dereferencing null pointer etc



                You can use code below to throw a Exception when this function triggered and Parameter is not of the SampleData1 type.



                 public object ProvideValue()
                {

                if (Parameter is SampleData1)
                {

                return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
                }
                else if (Parameter is SampleData2)
                {

                throw new Exception("Parameter must be of type SampleData1");
                }
                return "error";
                }





                share|improve this answer












                I think it is not possible to throw exception at compile time. The logical errors cannot be detected by the compiler, and hence they are detected only when the program is executed.



                Compile time error:



                If we do not follow the proper syntax and semantics of any programming language then the compiler throw compile time error.



                For example:



                1.Missing semicolon



                2.writing keywords in uppercase



                3.varaiable not defiend etc



                Runtime error:



                A runtime error is generated when the program is in running state. They are often termed as an exception.



                For example:



                1.Division by zero



                2.Running out of memory



                3.Dereferencing null pointer etc



                You can use code below to throw a Exception when this function triggered and Parameter is not of the SampleData1 type.



                 public object ProvideValue()
                {

                if (Parameter is SampleData1)
                {

                return Parameter.GetData();//TODO: throw Exception("Parameter must be of type SampleData1")
                }
                else if (Parameter is SampleData2)
                {

                throw new Exception("Parameter must be of type SampleData1");
                }
                return "error";
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 1 hour ago









                jack Hua

                3146




                3146






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53343545%2fimarkupextension-check-property-type-during-compilation-time-xamarin-forms%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    Origin of the phrase “under your belt”?