前提・実現したいこと
Visual Studio 2017のC#で、UWPでカスタム属性を作成したい。
学習用のプログラムを作って実行してみましたが、エラーが発生し実行できません。
下にソースコードを載せておきます。
よろしくお願います。
###エラー
CS0117 'Attribute' に 'GetCustomAttribute' の定義がありません
###調べたこと
”UWP カスタム属性”や”UWP 属性”でGoogle検索しましたが、該当するものを見つけることができませんでした。
該当のソースコード
MainPage.xaml.cs
C#
1using System; 2using Windows.UI.Popups; 3using Windows.UI.Xaml; 4using Windows.UI.Xaml.Controls; 5 6// 空白ページの項目テンプレートについては、https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x411 を参照してください 7 8namespace App1 9{ 10 /// <summary> 11 /// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。 12 /// </summary> 13 public sealed partial class MainPage : Page 14 { 15 public MainPage() 16 { 17 this.InitializeComponent(); 18 } 19 20 //----------追加した部分-------------------ここから----------------- 21 class Test2Attribute : Attribute 22 { 23 private string val; 24 25 public Test2Attribute(string v) 26 { 27 val = v; 28 } 29 30 public string Value 31 { 32 get { return val; } 33 } 34 } 35 36 //属性適用 37 [Test2("あああ")] 38 public class TestClass 39 { 40 } 41 42 private async void Button_Click(object sender, RoutedEventArgs e) 43 { 44 //属性取得 ↓↓↓↓↓↓↓↓↓↓↓↓ここでエラー発生↓↓↓↓↓↓↓↓↓↓ 45 var att = (Test2Attribute)Attribute.GetCustomAttribute(typeof(TestClass), typeof(Test2Attribute)); 46 47 await new MessageDialog(att.Value).ShowAsync(); 48 } 49 //-----------------------------ここまで----------------------------------- 50 51 } 52}
MainPage.xaml
C#
1<Page 2 x:Class="App1.MainPage" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="using:App1" 6 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 7 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 8 mc:Ignorable="d" 9 Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 10 11 <Grid> 12 <Button Content="Button" HorizontalAlignment="Left" Height="109" Margin="299,284,0,0" VerticalAlignment="Top" Width="220" Click="Button_Click"/> 13 14 </Grid> 15</Page>
開発環境
Microsoft Windows 10 Home (Version 10.0.17134 Build 17134)
Microsoft Visual Studio Community 2017 (Version 15.9.2)
Microsoft.NET Framework (Version 4.7.03056)
回答1件
あなたの回答
tips
プレビュー