标签: as-operator

结构上的as运算符?

我不明白.该As操作:

as运算符用于在兼容引用或可空类型之间执行某些类型的转换.

那为什么以下工作呢?

struct Baby : ILive
{
    public int Foo { get; set; }

    public int Ggg() 
    {
        return Foo;
    }
}

interface ILive
{
    int Ggg();
}

void Main()
{
    ILive i = new Baby(){Foo = 1} as ILive;    // ??????
    Console.Write(i.Ggg());                    // Output: 1
}
Run Code Online (Sandbox Code Playgroud)
  • Baby是一个结构,创建它将把价值放入stack.这里没有参考.

  • 这里肯定没有可空类型.

关于我为什么错的任何解释?

.net c# casting as-operator

24
推荐指数
2
解决办法
611
查看次数

为什么我不能使用as关键字作为结构?

我定义了以下结构:

public struct Call
{
    public SourceFile caller;
    public SourceFile callee;

    public Call(SourceFile caller, SourceFile callee)
    {
        this.caller = caller;
        this.callee = callee;
    }
}
Run Code Online (Sandbox Code Playgroud)

稍后,我将它分配给另一个对象的Tag属性:

line.Tag = new Call(sf1, sf2);
Run Code Online (Sandbox Code Playgroud)

但是当我尝试像这样检索Tag属性时,

Call call = line.Tag as Call;
Run Code Online (Sandbox Code Playgroud)

Visual Studio提供以下编译时错误:

必须在引用类型或可空类型中使用运算符

那是什么意思?我该如何解决?

.net c# type-systems casting as-operator

23
推荐指数
3
解决办法
1万
查看次数

什么时候应该在C#中使用as关键字

当你想在大多数时候想要改变类型时,你只想使用传统的演员.

var value = (string)dictionary[key];
Run Code Online (Sandbox Code Playgroud)

这很好,因为:

  • 它很快
  • 如果出现问题,它会抱怨(而不是给对象是空的例外)

那么使用什么是一个很好的例子as我无法真正找到或想到适合它的东西?

注意:实际上我认为有时会出现编译器阻止使用强制转换的情况as(泛型相关?).

.net c# as-operator c#-3.0

20
推荐指数
4
解决办法
1万
查看次数

当右侧操作数是通用的时,如何翻译"as"运算符?

我刚刚发布了这个问题答案,但我并不完全相信我的答案.我想知道有两件事,请考虑以下代码:

class Foo<T>
{ 
    void SomeMethod()
    {
        string str = "foo";
        Foo<T> f = str as Foo<T>;
    }
}
Run Code Online (Sandbox Code Playgroud)

据说C# Specification 5.0,有两种不同的转换方式as operator.

如果编译时类型E不是dynamic,则操作E as T产生与之相同的结果

E is T ? (T)(E) : (T)null
Run Code Online (Sandbox Code Playgroud)

如果编译时类型Edynamic,则与as operator强制转换运算符不同,它不是动态绑定的(第7.2.2节).因此,在这种情况下的扩展是:

E is T ? (T)(object)(E) : (T)null
Run Code Online (Sandbox Code Playgroud)

因为,这是无效的 (Foo<T>)str

str is Foo<T> ? (Foo<T>)str : (Foo<T>)null;
Run Code Online (Sandbox Code Playgroud)

我认为它应该被翻译为:

str is Foo<T> ? (Foo<T>)(object)str : …
Run Code Online (Sandbox Code Playgroud)

.net c# casting dynamic as-operator

17
推荐指数
1
解决办法
283
查看次数

具有可空值类型的as运算符是否不必要地慢?

考虑以下代码:

static void FillUsingAsNullable()
{
  int?[] arr = new int?[1 << 24];
  var sw = System.Diagnostics.Stopwatch.StartNew();
  for (int i = 0; i < arr.Length; ++i)
    arr[i] = GetObject() as int?;
  Console.WriteLine("{0:N0}", sw.ElapsedTicks);
}

static void FillUsingOwnCode()
{
  int?[] arr = new int?[1 << 24];
  var sw = System.Diagnostics.Stopwatch.StartNew();
  for (int i = 0; i < arr.Length; ++i)
  {
    object temporary = GetObject();
    arr[i] = temporary is int ? (int?)temporary : null;
  }
  Console.WriteLine("{0:N0}", sw.ElapsedTicks);
}

static object GetObject()
{
//Uncomment …
Run Code Online (Sandbox Code Playgroud)

c# performance nullable as-operator

8
推荐指数
1
解决办法
169
查看次数

IConfiguration.GetSection() 作为属性返回 null

我正在尝试检索应用程序内的配置。

我已将 IConfiguration 传递到需要提取一些设置的服务类中。

这个类看起来有点像这样:

private IConfiguration _configuration;
public Config(IConfiguration configuration)
{
    _configuration = configuration;
}
public POCO GetSettingsConfiguration()
{
    var section = _configuration.GetSection("settings") as POCO;

    return section;
}
Run Code Online (Sandbox Code Playgroud)

在调试中,我可以看到 _configuration 确实包含设置,但我的“部分”只是返回为 null。

我知道我可以尝试设置要在启动时创建的 Poco,然后作为依赖项注入,但由于设置的原因,如果可能的话,我宁愿在与注入的 IConfiguration 不同的类中进行操作。

我的 appsettings.json 具有以下值:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",

  "settings": {
    "Username": "user",
    "Password": "password",
    "Domain": "example.com",
    "URL": "http://service.example.com"
  }

}
Run Code Online (Sandbox Code Playgroud)

我的 poco 类如下所示:

public class POCO
{
    public string URL { get; set; }
    public string Username …
Run Code Online (Sandbox Code Playgroud)

c# as-operator asp.net-core

8
推荐指数
2
解决办法
6274
查看次数

将一个对象转换为另一种类型

我有一个名为A的班级和一个班级B.

public class A : UserControl { }

public class B : UserControl { }
Run Code Online (Sandbox Code Playgroud)

现在我有一个程序集,其类的函数接受A类的对象.这个程序集不是由我创建的,所以我没有任何控制.基本上它是第三方组装.

但我想提供我的B类对象,因为它是有点自定义的.请放心,它包含A类的所有属性.如何将类B的对象类型转换为A类,以便我可以在项目中集成第三方程序集,并根据我的需要自定义外观和感觉?

如果我这样的东西(A)objB是不允许的.然后我尝试了这个:

UserControl control = objB as UserControl;

A objA = control as A;
Run Code Online (Sandbox Code Playgroud)

但是在这种情况下的问题是objA是null.

为避免混淆:A类和汇编由第三方提供.

提前致谢 :)

c# user-controls casting as-operator

5
推荐指数
1
解决办法
6223
查看次数

如何一次使用"Asder"参数和"As"运算符多个类?

在Delphi中,有时我们需要这样做......

function TForm1.EDIT_Click(Sender: TObject);
begin
  (Sender As TEdit).Text := '';
end;
Run Code Online (Sandbox Code Playgroud)

...但有时我们需要重复其他对象类的功能,如...

function TForm1.COMBOBOX_Click(Sender: TObject);
begin
  (Sender As TComboBox).Text := '';
end;
Run Code Online (Sandbox Code Playgroud)

......因为运营商As不接受灵活性.它必须知道这个类,以便允许.Text它来之后().

有时候代码会变得类似functions,procedures因为我们需要使用类似的视觉控件来做同样的事情,而这些控件是我们无法指定的.

这只是一个使用示例.通常,我在更复杂的代码上使用这些代码来实现许多控件和其他类型对象的标准目标.

是否有替代或技巧使这些任务更灵活?

delphi oop sender as-operator

4
推荐指数
3
解决办法
2万
查看次数

如何将Action <Derived>转换为Action <Base>

public class SettingsBase
{

}
public class SettingsDerived : SettingsBase
{

}
public class yyy
{
    public void StartUpMethod(Action<SettingsDerived> settings)
    {
        //something goes here..
        SettingsDerived ds = new SettingsDerived();
        settings(ds);//take out SettingsDerived properties..
        SettingsBase bs = Method1(settings as Action<SettingsBase>);//as operator returns null because settings is not of type Action<SettingsBase>
        //again do something on s based on extracted SettingsDerived

    }

    public SettingsBase Method1(Action<SettingsBase> settings)
    {
        SettingsBase s = new SettingsBase();
        //something goes here
        settings(s);
        return s;
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎么做到这一点?或任何工作?

c# delegates as-operator

0
推荐指数
1
解决办法
685
查看次数