Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem might be more clear if you used explicit types instead of <code>var</code>. Declaring a variable with <code>var</code> does not make the variable type dynamic, it just makes the compiler figure out what specific type the variable needs to be (and it can be dangerous because you don't know as clearly what type it determines).</p> <p>So, having declared variables with a specific type (whether you know what that type is or not), you can then only access what that declared type knows unless you cast it appropriately.</p> <p>I think in the end there is no way to accomplish exactly what you want without specifying a type at some point as a type argument (in angle-brackets) or an explicit cast. You apparently want to convert a non-specific iherited/implementing type to a handy specific type, but that requires telling the complier a specific type for it to become.</p> <p>But you may get closer to what you'd like by changing your approach. What if you used a non-generic <code>IHierarchy</code>? (also correct the spelling) If...</p> <pre><code>public inetrface IHierarchy : IHierarchyItem { IHierarchy Parent { get; set; } IList&lt;IHierarchy&gt; Children { get; set; } } </code></pre> <p>...then any <code>IHierarchy node</code> variable could access <code>node.Parent</code> and <code>node.Children</code>... and also <code>node.Id</code> and <code>node.Title</code> because the <code>IHierarchyItem</code> is required and thus is known to <code>IHierarchy</code>.</p> <p>This approach would handle the hierarchy aspects easily and allows polymorphism in your <code>WorkContext.Current</code> (etc) return values, but it would require explicit casting from there to access anything specific to a class outside of the defined members of <code>IHierarchy</code>. It's not clear how much of an issue that might be for your purpose.</p> <p>You could also perhaps layer a generic <code>IHierarchy&lt;T&gt; : IHierarchy</code> on top of it to allow handling by a specific type without further casting. You might have to define one or both interface members explicitly rather than implicitly (in implementing classes) to avoid name collisions on the properties without generic type arguments.</p> <p><strong>EDITED TO ADD:</strong></p> <p>For example, something like:</p> <pre><code>public interface IHierarchy&lt;T&gt; : IHierarchy // Implies IHierarchyItem where T : IHierarchy&lt;T&gt; { ... } // As you had it. </code></pre> <p>Then in your implementation class:</p> <pre><code>public SiteMapNode : IHierarchy&lt;SiteMapNode&gt; // Implies IHierarchy { private SiteMapNode _Parent; private IList&lt;SiteMapNode&gt; _Children; // Implicit implement of IHierarchyItem and members of SiteMapNode itself. int Id { get; set; } string Title { get; set; } // Implicit implementation of IHierarchy&lt;SiteMapNode&gt; members // These are also members of SiteMapNode itself. SiteMapNode Parent { get { return _Parent; } set { _Parent = value; } } IList&lt;SiteMapNode&gt; Children { get return _Children; set _Children = value; } // Explicit implementation of IHierarchy members // The interface prefix is required to distinguish these from the // type-specific members above to declare different return types. IHierarchy IHierarchy.Parent { get { return _Parent; } // Might need (IHierarchy) cast set { Parent = (SiteMapNode)value; } } IList&lt;IHierarchy&gt; IHierarchy.Children { get { return _Children; } // Might need (IList&lt;IHierarchy&gt;) cast set { _Children = (IList&lt;SiteMapNode&gt;)value; } } } </code></pre> <p>(Or get fancier and have more sanity-checking in the IHierarchy implementations.) I might have missed some other explicit casting needed also; I'm not positive that the lists can be cast directly in this way, but I think by having IHierarchy&lt;T&gt; inherit IHierarchy it ensures that SiteMapNode is a valid IHierarchy and thus the elements of the list work for both list types). If that list casting doesn't work, you may have to create a custom generic collection class to manage the children as both unspecified IHierarchy and as generic IHierarchy&lt;T&gt;.</p> <p>For performance reasons you may want to add <code>IHierarchy _CastParent;</code> and <code>IList&lt;IHierarchy&gt; _CastChildren;</code> members and save the unspecified IHierarchy casts of these objects to avoid having to repeatedly recast them. I suggest that you always cast to the specific types (when setting from the unspecified) but you might defer casting from the specific types to the unspecified references until they are actually needed.</p> <p>Now, all that, of course, is only if this extra complexity is actually helpful for what you need. It would allow you to handle the hierarchy objects as unspecified types (which might be cast more specific later, or never) or as specific or generic hierarchy types which would preserve their type knowledge for handling without casting. You would still need to cast to a specific type at some point to convert from an unspecified IHierarchy return type if you wanted to then handle them as the type-specific hierarchy.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload