If an class implements an interface, and is assigned to a variable with a type of that interface, what type is that obje
Date : March 29 2020, 07:55 AM
this will help The runtime type of the object remains Motor even when you assign it to the variable statically typed as the interface Measurable. The type defines the behavior, while the interface defines which methods you can call without a cast.
|
Convert from class to Interface with a generic type interface Interface<Interface>
Date : March 29 2020, 07:55 AM
it fixes the issue In order to make it work, you need to make your generic interfaces covariant. But since ICollection is not covariant, it would require also changing the Children type to IReadOnlyCollection and remove the setters if you can afford that.public interface ITaxonomyHasChildren<out TChild> : ITaxonomy where TChild : ITaxonomy
{
IReadOnlyCollection<TChild> Children { get; }
}
public interface ITaxonomyHasParent<out TParent> : ITaxonomy where TParent : ITaxonomy
{
TParent Parent { get; }
}
private void SelectedEntityChanged(ITaxonomy selectedEntity)
{
CommonName = selectedEntity.CommonName;
ScientificName = selectedEntity.ScientificName;
Children = (selectedEntity as ITaxonomyHasChildren<ITaxonomy>)?.Children;
Parent = (selectedEntity as ITaxonomyHasParent<ITaxonomy>)?.Parent;
}
class Taxonomy : ITaxonomy, ITaxonomyHasChildren<Taxonomy>, ITaxonomyHasParent<Taxonomy>
{
public string CommonName { get; set; }
public string ScientificName { get; set; }
public Taxonomy Parent { get; set; }
public List<Taxonomy> Children { get; set; } = new List<Taxonomy>();
IReadOnlyCollection<Taxonomy> ITaxonomyHasChildren<Taxonomy>.Children => Children;
}
|
.NET Core Cast to interface with generic type parameter of type which implements interface
Date : March 29 2020, 07:55 AM
this will help Why don't you add a non-generic interface too: interface IInterface { void Run(IParameter input); }
|
Create record from interface with return type as property type from original interface
Date : March 29 2020, 07:55 AM
it should still fix some issue I have an interface like , You can use a mapped type for this: type Functionify<T> = { [K in keyof T]: () => T[K] }
|
Converting argument $1 type: unsupported type []interface {}, a slice of interface
Date : March 29 2020, 07:55 AM
may help you . Wrap the slice with pq.Array. Change IN ($1) to = any($1) so the array value can be used. Here's the code: stm := `SELECT
U.id,
(
CASE WHEN TRIM(UP.first_name || ' ' || UP.last_name) = '' THEN U.id :: TEXT ELSE UP.first_name || ' ' || UP.last_name END
) as avatar_name,
FROM
users AS U
JOIN user_profiles UP ON UP.user_id = U.id
WHERE
U.id = any($1);`
rows, err := postgresql.Instance.Query(stm, pq.Array(userIDs))
|