193

私は最近このコードを見つけました:

 public static implicit operator XElement(XmlBase xmlBase)
 {
     return xmlBase.Xml;
 }

どういうstatic implicit operator意味ですか?

4

5 に答える 5

320

This is a conversion operator. It means that you can write this code:

XmlBase myBase = new XmlBase();
XElement myElement = myBase;

And the compiler won't complain! At runtime, the conversion operator will be executed - passing myBase in as the argument, and returning a valid XElement as the result.

It's a way for you as a developer to tell the compiler:

"even though these look like two totally unrelated types, there is actually a way to convert from one to the other; just let me handle the logic for how to do it."

于 2010-11-25T04:38:04.377 に答える
15

XmlBaseこのような暗黙の演算子は、暗黙的に変換できることを意味しますXElement

XmlBase xmlBase = WhatEverGetTheXmlBase();
XElement xelement = xmlBase;   
//no explicit convert here like: XElement xelement = (XElement)xmlBase;
于 2010-11-25T04:36:53.313 に答える
11

もう1つの興味深い使用法は、(Unityがオブジェクト(したがってMonoBehaviorのインスタンス)がnullかどうかをチェックするために行った)です。

public static implicit operator bool (CustomClass c)
{
    return c != null;
}

コードはクラス(この場合はCustomClass)内にある必要があることに注意してください。そうすれば、次のようなことができます。

void Method ()
{
    CustomClass c1 = null;
    CustomClass c2 = new CustomClass ();

    bool b1 = c1; // is false
    bool b2 = c2; // is true

    if (!c1 && c2)
    {
        // Do stuff
    }
}

明らかに、最も悪名高い使用法は、クラスの1つを別のクラスに変換するためにそれを使用することかもしれません。しかし、基本的なタイプでそれらを使用することも検討する価値があります...そして私はそれが非常にまれに言及されているのを見ます。

于 2018-07-10T14:42:39.407 に答える
4

これは暗黙の変換演算子です((type)変換構文を必要とする明示的な演算子とは対照的です)

于 2010-11-25T04:37:00.813 に答える
0

私の2セント。

これは、ビルダーパターンで使用される不変エンティティの単体テストに役立ちます。

従業員ドメインオブジェクトが不変の方法で定義されているとします。これは通常、DDDスタイルに準拠する場合に行います。

public class Employee 
{ 
  public Employee(int id, string firstname, string lastname, DateTime birthdate, string street) 
  { 
    this.ID = id; 
    this.FirstName = firstname; 
    this.LastName = lastname; 
    this.BirthDate = birthdate; 
    this.Street = street; 
  } 

  public int ID { get; private set; } 
  public string FirstName { get; private set; } 
  public string LastName { get; private set; }
  public DateTime BirthDate { get; private set; } 
  public string Street { get; private set; } 

  public string getFullName() 
  { 
    return this.FirstName + " " + this.LastName; 
  } 

  public int getAge() 
  { 
    DateTime today = DateTime.Today;
    int age = today.Year - BirthDate.Year;
    
    if (BirthDate > today.AddYears(-age))
      age--;

    return age; 
  } 
}

これで、次のような従業員ビルダーを作成できます(テストプロジェクト内)。最後に、この暗黙の演算子があることに注意してください。

public class EmployeeBuilder
{ 
  private int id = 1; 
  private string firstname = "first"; 
  private string lastname = "last"; 
  private DateTime birthdate = DateTime.Today; 
  private string street = "street"; 
  public Employee Build() 
  { 
    return new Employee(id, firstname, lastname, birthdate, street); 
  } 
  public EmployeeBuilder WithFirstName(string firstname) 
  { 
    this.firstname = firstname; 
    return this; 
  } 
  public EmployeeBuilder WithLastName(string lastname) 
  { 
    this.lastname = lastname; 
    return this; 
  } 
  public EmployeeBuilder WithBirthDate(DateTime birthdate) 
  { 
    this.birthdate = birthdate; 
    return this; 
  } 
  public EmployeeBuilder WithStreet(string street) 
  { 
    this.street = street; 
    return this; 
  } 
  public static implicit operator Employee(EmployeeBuilder instance) 
  { 
    return instance.Build(); 
  } 
}

これで、次のような従業員テストクラスを受講できます。

public class EmployeeTest
{
  [Test]
  public void GetFullNameReturnsCombination()
  { 
    // Arrange
    Employee emp = new EmployeeBuilder().WithFirstName("Vivek").WithLastName("Koppula"); 
    // Act
    string fullname = emp.getFullName(); 
    // Assert
    Assert.That(fullname, Is.EqualTo("Vivek Koppula")); 
  } 

  [Test] 
  public void GetAgeReturnsCorrectValue() { 
  // Arrange
  Employee emp = new EmployeeBuilder().WithBirthDate(new DateTime(1983, 1,1)); 
  // Act
  int age = emp.getAge(); 
  // Assert
  Assert.That(age, Is.EqualTo(DateTime.Today.Year - 1983)); 
  } 
}

これにより、必要なパラメーターだけで従業員を作成できるため、単体テストの作成が容易になります。

たとえば、最初のテストでは、名と名前のみが関係します。したがって、最初のケースでは、年齢や通りに煩わされる必要はありません。

同様に、2番目のケースでは、年齢だけに関心があります。

記事の参照。

  1. 柔軟で表現力豊かなユニットテストとビルダーパターン
  2. テストデータのビルダーパターンを使用したテストの改善
于 2022-01-30T07:36:53.360 に答える