如何自动扩展一种类型?

huangapple go评论64阅读模式
英文:

How to automatically expand a type?

问题

我计划将实体类后代对象传递给一个函数,并调用相同函数的唯一实现,但我无法在没有强类型的情况下找出如何做到这一点。我希望对每种继承者的类型都有不同的行为。

public IRepository<Entity> choose(Entity entity) 
{
    return _repositories.FirstOrDefault(h => h.GetType().Namespace == entity.getStorePlace(((WeatherEntity)entity).getRealStorePlace)); 
}


public class Entity : IEntity
{
    public string Id { get; set; }

    public string getStorePlace(Func<string> f)
    {
        return f();
    }
}


public class WeatherEntity : Entity, IweatherEntity
{
        public string name { get; set; } = null!;
        public int temperature { get; set; }
        public string location { get; set; } = null!;
        public bool isRainyToday { get; set; } = false;
       
        public  string  getRealStorePlace()
        {
                if (temperature > 25)
                {
                        return "ConsoleAppTest.Repositories.MongoRepositories";

                }
                return "ConsoleAppTest.Repositories.JsonRepositories";
        }
}

我尝试在实体类中添加唯一函数,并在WeatherEntity类中进行覆盖,但结果相同。

英文:

I plan to pass Entity-class descendant objects to a function and call a unique implementation of the same function, but I can't figure out how to do it without strong typing. I want to have different behavior for each type of inheritor.

public IRepository<Entity> choose(Entity entity) 
{
    return _repositories.FirstOrDefault(h => h.GetType().Namespace == entity.getStorePlace(((WeatherEntity)entity).getRealStorePlace)); 
}


public class Entity : IEntity
{
    public string Id { get; set; }

    public string getStorePlace(Func<string> f)
    {
        return f();
    }
}


public class WeatherEntity : Entity, IweatherEntity
{
        public string name { get; set; } = null!;
        public int temperature { get; set; }
        public string location { get; set; } = null!;
        public bool isRainyToday { get; set; } = false;
       
        public  string  getRealStorePlace()
        {
                if (temperature > 25)
                {
                        return "ConsoleAppTest.Repositories.MongoRepositories";

                }
                return "ConsoleAppTest.Repositories.JsonRepositories";
        }
}

I tried to add unique function to the Entity class and override it in WeatherEntity Class, but got the same result.

答案1

得分: 0

我将以下部分翻译为中文:

我将 Entity 类更改为抽象类,现在它正常工作。

public IRepository<Entity> choose(Entity entity) 
{
    return _repositories.FirstOrDefault(h => h.GetType().Namespace == entity.getStorePlace(entity.getRealStorePlace)); 
}

public abstract class Entity : IEntity
{
    public string Id { get; set; }

    public string getStorePlace(Func<string> f)
    {
        return f();
    }

    public abstract string getRealStorePlace();
}

public class WeatherEntity : Entity, IweatherEntity
{
        
     
        public string name { get; set; } = null!;

        public int temperature { get; set; }

        public string location { get; set; } = null!;

        public bool isRainyToday { get; set; } = false;
       
          public override string  getRealStorePlace()
        {
                if (temperature > 25)
                {
                        return "ConsoleAppTest.Repositories.MongoRepositories";

                }
                return "ConsoleAppTest.Repositories.JsonRepositories";
        }

}

请注意,我已将代码中的 HTML 实体代码(如 &lt;&gt;&quot;)还原为正常的代码。

英文:

I changed Entity class to abstracted and it workes now.

 public IRepository&lt;Entity&gt; choose(Entity entity) 
    {
        return _repositories.FirstOrDefault(h =&gt; h.GetType().Namespace == entity.getStorePlace(entity.getRealStorePlace)); 
    }

public abstract class Entity : IEntity
{
    public string Id { get; set; }

    public string getStorePlace(Func&lt;string&gt; f)
    {
        return f();
    }

    public abstract string getRealStorePlace();
}


public class WeatherEntity : Entity, IweatherEntity
{
        
     
        public string name { get; set; } = null!;

        public int temperature { get; set; }

        public string location { get; set; } = null!;

        public bool isRainyToday { get; set; } = false;
       
          public override string  getRealStorePlace()
        {
                if (temperature &gt; 25)
                {
                        return &quot;ConsoleAppTest.Repositories.MongoRepositories&quot;;

                }
                return &quot;ConsoleAppTest.Repositories.JsonRepositories&quot;;
        }

}

huangapple
  • 本文由 发表于 2023年7月3日 16:00:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定