本文介绍了C#中的Const对象作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,您可以将一个const对象传递给函数。这会强制代码将该对象视为只读(忽略使用强制转换可以去除常量的事实)。我正在尝试用C#做同样的事情。我有一个用户将实现的界面。其中一种方法将被赋予一个对象作为参数。如何在方法范围内使对象'const',否则变为可变。

In C++, you can pass into a function an object that is const. This forces the code to treat that object as 'read only' (ignoring the fact that you can strip constness away using casts). I'm trying to do the same with C#. I have an interface that the user will implement. One of the methods will be given an object as a parameter. How do I make the object 'const' in the scope of the method and mutable otherwise.

解决方案:接口。我创建了一个界面,其中属性上唯一可用的操作是'get'。

Solution: interfaces. I make an interface where the only action available on a property, is 'get'.

public interface IVal { ... }


public interface IFoo
{
    IVal Val { get; }
}

public class Foo : IFoo
{
    public IVal Val { get; }
}

我通过界面传递,一切都很棒。但是,在我的具体课程中,我希望有一个设置。它允许我设置一个具体的类值。我的具体课程怎么样?

I pass by interface and everything is amazing. However, in my concrete class, I would like to have a 'set' available. It would allow me to set a concrete class Value. How would my concrete class look like?

我在考虑使用'internal'关键字。我读到它将方法/属性暴露给您自己的程序集中的代码。这是我的答案吗?

I was thinking of using the 'internal' keyword. I read that it exposes the method/property to code within your own assembly. Is this my answer?

编辑:为了更好地说明这一点,我添加了一个自动为我投射的二传手。但是,对于每个属性,这是很多重复代码:

To illustrate this better, I added a setter that would automatically cast for me. However, for each property, this is a lot of repetative code:

private Val _val;
public IVal Val
{
    get { return this._val; }
    set { this._val = value as Val; }
}


推荐答案

你可以添加设置,它将正常工作:

You can just add the set and it will work fine:

public IValue Value { get; set; }

像这样,你可以设置值,如果你有 Foo ,但如果您有 IFoo ,则不会。与C ++案例类似,如果你有 IFoo ,你可以转换为 Foo (只要对象实际上是 Foo )然后使用setter。

Like this, you can set the value if you have Foo, but not if you have IFoo. Similar to the C++ case, if you have IFoo, you can cast to Foo (as long as the object actually is Foo) and then use the setter.

另一种选择是使setter 受保护 private

Another option is to make the setter protected or private:

public IValue Value { get; private set; }

这使你可以确保只有这个类中的代码(或者在类的情况下继承了 protected )可以设置属性。

This allows you to make sure that only code in this class (or inherited classed in the case of protected) can set the property.

关于 internal :是的,它只将成员暴露给同一程序集(和朋友程序集)中的代码。但这并不经常使用。如果您没有指定该类是 public ,那么使用(和有用)的是内部类型,甚至是默认类型。

Regarding internal: yes, it exposes the member only to code in the same assembly (and friend assemblies). But this is not used very often. What is used (and useful) are internal types, which is even the default, if you don't specify that that class is public.

这可以帮助您解决问题。如果你使 Foo internal ,其他程序集中的方法 IFoo 将无法设置属性,即使进行转换也是如此。

This could help you with your problem. If you make Foo internal, a method in other assembly that has IFoo won't be able to set the property, even with casting.

当然,代码总是可以使用反射来设置属性。但是你大部分时间都不应该担心。

Of course, the code could always use reflection to set the property. But you shouldn't worry about that most of the time.

这篇关于C#中的Const对象作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:52