Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Stymied?
Message
De
26/07/2011 14:09:52
 
 
À
26/07/2011 13:09:19
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Titre:
Versions des environnements
Environment:
C# 4.0
Divers
Thread ID:
01519092
Message ID:
01519243
Vues:
31
>>>>>>Assume this class:
public partial class Library : EntityObject
>>>>>>    {
>>>>>>         public Library Parent
>>>>>>        {
>>>>>>            get { return new Library();}
>>>>>>         }
>>>>>>    }
As you may guess given the EntityObject inheritance the really thing is more complex, generated by the EDMX builder and cannot be modified. I'd like it to implement an Interface:
public interface ITreeview
>>>>>>    {
>>>>>>        ITreeview Parent { get; }
>>>>>>    }
But (I suppose expectedly) adding this won't compile:
public partial class Library : ITreeview
>>>>>>    {
>>>>>>    }
Is there anyway of achieving what I want ?
>>>>>
>>>>>And yes, that should compile, as long as you implement ITreeview in your custom partial class..
>>>>
>>>>The Library class (as in the first snippet above) has the 'Parent' property which is an instance of the Library class - but to compile it would have to be defined as ITreeview.Parent
>>>
>>>Would this work for you?
>>>
>>>
using System;
>>>using System.Collections.Generic;
>>>using System.Linq;
>>>using System.Text;
>>>
>>>namespace ConsoleApplication1
>>>  {
>>>  class Program
>>>    {
>>>    static void Main(string[] args)
>>>      {
>>>      Library lib = new Library();
>>>      ITreeview itv = (ITreeview)lib;
>>>      Console.WriteLine(((Library)itv.Parent).Name);
>>>      Console.ReadLine();
>>>      }
>>>    }
>>>
>>>  public interface ITreeview
>>>    {
>>>    ITreeview Parent { get; }
>>>    }
>>>
>>>  public partial class Library
>>>    {
>>>    public Library Parent { get { return new Library(); } }
>>>    public string Name { get { return "Hello World"; } }
>>>    }
>>>
>>>  public partial class Library : ITreeview
>>>    {
>>>    ITreeview ITreeview.Parent
>>>      {
>>>      get { return this.Parent; }
>>>      }
>>>    }
>>>  }
>>>
>>
>>I could use explicit implementations but there are several classes that will need to implement the interface and, of course, the interface itself is much more complex than in the above example. The end result would be my writing a lot of manual code into a lot of partial classes rather than just being able to mark a stub partial class as implementing the interface.
>>
>>I'm wondering if this problem is really a limitation of the compiler. If it *did* compile I don't see why the resulting code would have any typing issues?
>>
>>Also, I don't really understand why an explicit implementation resolves the problem - this doesn't fit any of the usual cases where explicit implementation is required ?
>
>It does seem like a compiler limitation.
>
>If you write it the way I did it should just be a cut and paste, not lightweight and a bit of a pain, but you shouldn't need to rewrite it for each class.
>
>Unfortunately, I don't have a better suggestion.
>
>I have a VM base class that implements my treeview interface, I have to manually load the children, but other than that it's all coded.

You're right - it is doable with cut and paste. I guess I'll have to go with that.
I've got a generic base class for Treeviews. Lot of basic stuff in there like lazy loading of sub nodes then customized a bit depending on the actual type that is fed in.
I look at it now and find it hard to unravel what's going on but it seems to work :-}
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform