امکان تعریف سادهتر خواص Immutable در C# 9.0 با معرفی ویژگی خواص Init-Only
نویسنده: وحید نصیری
تاریخ: ۱۳۹۹/۰۸/۰۴ ۸:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class Person
{
public string _firstName;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
} public string _firstName;
public string FirstName
{
get
{
return _firstName;
}
private set
{
_firstName = value;
}
} public class Person
{
public string FirstName { get; set; }
} public class User
{
public string Name { get; private set; }
} public class User
{
public string Name { get; }
} public class Foo
{
public string FirstName { get; set; } = "Initial Value";
} public class Foo
{
public DateTime DateOfBirth { get; set; }
public int Age => DateTime.Now.Year - DateOfBirth.Year;
} public class User
{
public string Name { get; private set; }
} var user = new User
{
Name = "User 1" // Compile Error
}; The property or indexer 'User.Name' cannot be used in this context because the set accessor is inaccessible [CS9Features]csharp(CS0272)
public class User
{
public User(string name)
{
this.Name = name;
}
public void SetName(string name)
{
this.Name = name;
}
public string Name { get; private set; }
} public class User
{
public string Name { get; init; }
} var user = new User
{
Name = "User 1"
}; // Compile Time Error // Init-only property or indexer 'User.Name' can only be assigned in an object initializer, // or on 'this' or 'base' in an instance constructor or an 'init' accessor. [CS9Features]csharp(CS8852) user.Name = "Test";
public class User
{
public string Name { get; init; }
public User(string name)
{
this.Name = name; // Works fine
}
public void SetName(string name)
{
this.Name = name; // Compile Time Error
}
} public class Product
{
public Product(string name)
{
_name = name;
}
private readonly string _name;
public string Name => _name;
} public class Person
{
private readonly string _name;
public string Name
{
get => _name;
init => _name = value;
}
} PropertyInfo propertyInfo = typeof(Person).GetProperty(nameof(User.Name)); propertyInfo.SetValue(user, "edited"); Console.WriteLine(user.Name); // Print "edited"
public static bool IsInitOnly(this PropertyInfo propertyInfo)
{
MethodInfo setMethod = propertyInfo.SetMethod;
if (setMethod == null)
return false;
var isExternalInitType = typeof(System.Runtime.CompilerServices.IsExternalInit);
return setMethod.ReturnParameter.GetRequiredCustomModifiers().Contains(isExternalInitType);
} PropertyInfo propertyInfo = typeof(Person).GetProperty(nameof(Person.Name)); var isInitOnly = propertyInfo.IsInitOnly();
<LangVersion>9.0</LangVersion>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class IsExternalInit
{
}
}