Issue = new IdentifiableName { Id = AddIssue.Id }, 报错: error CS0272: 属性或索引器“Identifiable<IdentifiableName>.Id”不能用在此上下文中,因为 set 访问器不可访问;请给出解决方案
时间: 2023-12-14 13:40:22 浏览: 10
这个错误是因为在 `Identifiable<IdentifiableName>` 类中,`Id` 属性的 set 访问器被限制为内部访问。因此,从外部代码中无法通过 `Issue.Id` 的方式给 `Id` 属性赋值。
解决方案有两个:
1. 修改 `Identifiable<IdentifiableName>` 类的定义,将 `Id` 属性的 set 访问器改为公共访问。例如:
```
public class Identifiable<T>
{
public T Id { get; set; }
}
```
2. 在定义 `AddIssue` 对象时,使用构造函数或者对象初始化语法来设置 `Id` 属性的值。例如:
```
var issueId = new IdentifiableName { Id = "123" };
var addIssue = new AddIssue { Issue = issueId };
```
这样就可以避免直接访问 `Id` 属性的 set 访问器而导致编译错误。
相关问题
Redmine.Net.Api.Types.TimeEntry time_entries.Issue = new IdentifiableName { Id = AddIssue.Id }; 报错如何解决 error CS0272: 属性或索引器“Identifiable<IdentifiableName>.Id”不能用在此上下文中,因为 set 访问器不可访问
这个错误是由于 `IdentifiableName` 类中的 `Id` 属性的 `set` 访问器不可访问导致的。
可以尝试检查一下 `IdentifiableName` 类中 `Id` 属性的访问修饰符是否为 `private`,如果是,则需要将其修改为 `public` 或者添加一个 `public` 的 `set` 访问器来解决该问题。
例如:
```csharp
public class IdentifiableName
{
public int Id { get; set; }
}
```
如果 `IdentifiableName` 类已经是一个第三方库或者不能修改源代码的话,可以考虑使用反射来设置 `Id` 属性的值。
例如:
```csharp
var issue = new Redmine.Net.Api.Types.TimeEntry();
var identifiableName = new IdentifiableName { Id = AddIssue.Id };
var property = identifiableName.GetType().GetProperty("Id");
property.SetValue(identifiableName, 123); // 设置 Id 属性的值
issue.Issue = identifiableName;
```
with tf.variable_scope("encoder"):
Sure! Using `tf.variable_scope("encoder")` in TensorFlow allows you to create variables within a specific scope. This is helpful when you want to organize your variables and avoid naming conflicts.
For example, within the "encoder" scope, you can define your encoder variables like this:
```python
with tf.variable_scope("encoder"):
# define encoder variables here
encoder_w = tf.Variable(...)
encoder_b = tf.Variable(...)
...
```
By using the `tf.variable_scope("encoder")`, TensorFlow will automatically prepend the scope name to the variable names, making them unique and identifiable within the "encoder" scope.
Please let me know if you have any more questions!