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")
}
xamarin.forms compilation-time
add a comment |
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")
}
xamarin.forms compilation-time
add a comment |
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")
}
xamarin.forms compilation-time
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
xamarin.forms compilation-time
asked 2 days ago
g_m
234
234
add a comment |
add a comment |
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";
}
add a comment |
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";
}
add a comment |
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";
}
add a comment |
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";
}
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";
}
answered 1 hour ago
jack Hua
3146
3146
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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