切换对象类型之间的语句

时间:2018-04-12 14:25:02

标签: c# asp.net-mvc

我正在尝试使用switch语句来声明变量。根据我发送的情况,变量将是不同的对象类型。

${}

我遇到的问题是,当我尝试访问模型中的列时,我收到消息object savedKey = null; switch (type) { case "RegistrationKey": savedKey = db.RegistrationKey.FirstOrDefault(k => k.RegistrationKey == key); break; case "ClockKey": savedKey = db.ClockKey.FirstOrDefault(k => k.RegistrationKey == key); break; }

Cannot Resolve Symbol

无法找到注册码。如果我声明对象类型var decSavedkey = ConCryptor.Decrypt(savedKey.RegistrationKey);,那么我不会收到错误,但我的switch语句中的RegistrationKey savedKey = null;会返回错误,因为我将ClockKey声明为对象。 / p>

这就是使用savedKey的方式

RegistrationKey

4 个答案:

答案 0 :(得分:0)

您需要一些公共基类或接口,您的类派生自/ implement。由于object不知道关于RegistrationKey任何,因此您从数据库返回的效果应为Class1Class2类型:

interface MyInterface
{
    List<Key> Keys { get; set;}
}
public class Class1 : MyInterface
{
    public List<Key> Keys { get; set;}
}
public class Class2 : MyInterface
{
    public List<Key> Keys { get; set;}
}

现在您不需要任何切换,因为实例本身知道如何解决Key

MyInterface instance = db.QueryFeature(...);
savedKey = instance.Keys.FirstOrDefault(k => k.RegistrationKey == key);

答案 1 :(得分:0)

您可以创建一个声明ClockKeyRegistrationKey的公共属性的接口:

public interface IKeyProps
{
    string RegistrationKey {get;set;}
    DateTime ExpirationDate {get;set;}
}

让这两种类型实现它:

public class RegistrationKey : IKeyProps
{ /*...*/ }
public class CloseKey : IKeyProps
{ /*...*/ }

然后将savedKey变量声明为IKeyProps

IKeyProps savedKey = null;
switch (type)
{
    case "RegistrationKey":
        savedKey = db.RegistrationKey.FirstOrDefault(k => k.RegistrationKey == key);
        break;
    case "ClockKey":
        savedKey = db.ClockKey.FirstOrDefault(k => k.RegistrationKey == key);
        break;
}    

// this now works as IKeyProps declares a RegistrationKey property
var decSavedkey = ConCryptor.Decrypt(savedKey.RegistrationKey);

另外(如果您无法更改类声明),您可以将savedKey声明为dynamic。但是你失去了类型安全和智能感知支持等等。而且它比黑客解决方案还要糟糕。

答案 2 :(得分:0)

只需将单词“object”更改为该表类型的名称,或者如果类型不同,则将其更改为“dynamic”

答案 3 :(得分:0)

添加继承或接口来解决这个问题并没有什么本质上的错误,但我认为没有必要。

为每个DbSet项目获取的数据创建局部变量。

string registrationKey = string.Empty;
DateTime expirationDate = DateTime.Min;

switch (type)
{
    case "RegistrationKey":
        var registrationKeyItem = db.RegistrationKey.FirstOrDefault(k => k.RegistrationKey == key)
        if (registrationKeyItem != null)
        {
            registrationKey = registrationKeyItem.RegistrationKey;
            expirationDate = registrationKeyItem.ExpirationDate;
        }
        break;
    case "ClockKey":
        var clockKeyItem = db.ClockKey.FirstOrDefault(k => k.RegistrationKey == key);
        if (clockKeyItem != null)
        {
            registrationKey = clockKeyItem.RegistrationKey;
            expirationDate = clockKeyItem.ExpirationDate;
        }
        break;
}

然后使用检索到的数据调用您的逻辑。基本上,保持简单。

var sentKey = ConCryptor.Decrypt(key);
var decSavedkey = ConCryptor.Decrypt(registrationKey);
var today = DateTime.Now;
// lets validate some keys
if (sentKey == decSavedkey && 
    DateTime.Parse(expirationDate.ToString()) >= 
    DateTime.Parse(today.Date.ToString()))
{
     status = true;
}