2016-11-25 3 views
0

多くのフォントが含まれているレポートの設定オブジェクトがあります。フォントプロパティの設定者に古いフォントを処理する必要がありますか?

彼らは、この

public Font TitleFont { get; set; } = new Font("Arial", 8, FontStyle.Bold); 

のようにデフォルト設定されているが、彼らは、GDI +レポートのレンダリングに使用される前に、多くの場所で変更を取得することができます。これはwinFormsコントロールにはありません。

フォントはIDisposableを実装していますので、古いフォントをプロパティのセッターで処理する必要がありますか?または、フォント名、サイズ、スタイルを3つのプロパティとして保存し、必要なときにのみフォントを作成する必要がありますか?

+0

[コントロールのフォントを変更するときに古いフォントを処理する必要がありますか?](http://stackoverflow.com/questions/29103522/should-i-dispose-of-the-old-font-when-コントロールのフォントを変更する) – Jens

+0

@Jens私はそれが重複しているとは思わない、少なくともその一つではない。これはwinformsコントロールではなく、オブジェクトにはフォントの責任があります – dibs487

+0

"フォントに対する責任を負う"場合は、それを 'Dispose'する必要があります。単にそれを使用する場合(例えば、ヘルパークラスやルーチンを開発した場合など)、 –

答えて

0

フォントがの場合は、クラスがであることが主な質問です。はい、それはフォントDisposeすべき :

public class MyFontStorage: IDisposable { 
    private Font m_MyFont; 
    ... 
    public Font MyFont { 
    get { 
     return m_MyFont; 
    } 
    set { 
     if (m_MyFont == value) 
     return; 

     if (m_MyFont != null) 
     m_MyFont.Dispose(); 

     m_MyFont = value;    
    } 
    } 

    protected virtual void Dispose(bool disposing) { 
    if (disposing) { 
     MyFont = null; 
    } 
    } 

    public void Dispose() { 
    Dispose(this); 

    GC.SuppressFinalize(this); 
    } 
} 

を....

using (MyFontStorage storage = new MyFontStorage()) { 
    ... 
    // Storage is responsible for the font 
    storage.MyFont = new Font(...); 
    ... 
    // Control has responsibility for the font as well, that's why 
    // we have to create a copy in order to each font instance has one owner 
    MyControl.Font = new Font(MyFontStorage.MyFont, MyFontStorage.Font.Style); 
    ... 
} 

コピーを作成するだけではなく割り当てることが悪い習慣(エラーを起こしやすい)であり、それはですなぜ工場を実装したいのですか? -

"私はフォント名、サイズ、およびスタイルを保存しなければならない"

public class MyFontFactory { 
    private string m_FamilyName; 
    private float m_Size; 
    private FontStyle m_Style; 

    ... 

    public MyFontFactory(Font propotype) { 
    ... 
    m_FamilyName = propotype.FontFamily.Name; 
    m_Size = propotype.Size; 
    m_Style = propotype.Style; 
    } 

    public Font CreateFont() { 
    return new Font(m_FamilyName, m_Size, m_Style); 
    } 
} 

....

MyFontFactory factory = new factory(SomeControl.Font); 
... 
// Much more natural: 
// MyFontFactory creates a new font and MyControl takes responsibility for it 
MyControl.Font = factory.CreateFont(); 

最後に、ヘルパークラス/ユーティリティ/ルーチンなどの場合

public class MyFontMisc { 
    private Font m_MyFont; 

    public Font MyFont { 
    get { 
     return m_MyFont; 
    } 
    set { 
     m_MyFont = value;    
    } 
    } 

    // Just some computations 
    public float EmSizeInMillimeters { 
    get { 
     if (null == m_MyFont) 
     return 0.0; 

     ... 
    } 
    } 
} 

......

// MyFontMisc doesn't hold any responsibility for the font 
MyFontMisc misc = new MyFontMisc(SomeControl.Font); 

// MyFontMisc just uses font for some computation and doesn't take 
// any responsibility for the font (which owns to SomeControl) 
Length = misc.EmSizeInMillimeters * 3.0f + 15.0f; 
関連する問題