第一问:关于C#中的类,public class Test
{
public string x;
};
我在其他的类中Test fuck=new Test();了,居然用fuck.x还是不能进行赋值,从C++迁移到C#来还真是不适应,问了很多人,他们说非要在类里面加成这样public class Test
{
public string x;
public string HaHa
{
get { return x; }
set { x = value; }
}
};然后我只能访问Test.HaHa的说,真是这样吗?第二问:
接上面的问题衍生public class Test
{
public string[] x;
public string[] HaHa
{
get { return x; }
set { x = value; }
}
};这样get和set对于string的数组是否可行?第三问:
看到很多人的代码里面有“[method]”这样的,这个是不是类似注释一样的东西?不写反正是无所谓的?那[]里面我能不能随便写的?1问10分
{
public string x;
};
我在其他的类中Test fuck=new Test();了,居然用fuck.x还是不能进行赋值,从C++迁移到C#来还真是不适应,问了很多人,他们说非要在类里面加成这样public class Test
{
public string x;
public string HaHa
{
get { return x; }
set { x = value; }
}
};然后我只能访问Test.HaHa的说,真是这样吗?第二问:
接上面的问题衍生public class Test
{
public string[] x;
public string[] HaHa
{
get { return x; }
set { x = value; }
}
};这样get和set对于string的数组是否可行?第三问:
看到很多人的代码里面有“[method]”这样的,这个是不是类似注释一样的东西?不写反正是无所谓的?那[]里面我能不能随便写的?1问10分
是可以访问的。改成这样就不能访问
public class Test
{
private string x;
}
,需要改成
public class Test
{
private string x;
public string HaHa
{
get { return x; }
set { x = value; }
}
}
你总是习惯使用public修饰符, x应当用private
public class Test
{
public string x;
};----------
可以访问x,但不能为其赋值或取值操作。。
比如在webservice一个方法加上[WebMethod]系统就知道它是可以远程调用的
从Attribute类继承你也可以自定义特性,
比如你建一个类叫XxxAttribute,就可以这么用了[Xxx]
{
public string x;
};
x是类Test的属性,属性的复值应用方法
get,,,set
{
public string[] x;
public string[] HaHa
{
get { return x; }
set { x = value; }
}
};我觉得这样应该不行把,,
应该要foreach语句把,,
到机子上去试试~~~~~!
(我已经试过了) class Program
{
public static void Main(string[] args)
{
Test t = new Test();
t.S = new string[]{"s","sss"};
Console.WriteLine(t.S.ToString());
Console.ReadKey();
}
}
class Test
{
private string[] s; public string[] S
{
get { return s; }
set { s = value; }
}
}
public class Test
{
public string x;
}
其实就是指
为指定的方法或是类加上以下的属性类MethodAttributes指定方法属性的标志。这些标志在 corhdr.h 文件中定义。此枚举有一个 FlagsAttribute 属性,该属性使其成员值按位组合。命名空间: System.Reflection
程序集: mscorlib(在 mscorlib.dll 中)语法
Visual Basic(声明)
<SerializableAttribute> _
<FlagsAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration MethodAttributes
Visual Basic(用法)
Dim instance As MethodAttributes
C#
[SerializableAttribute]
[FlagsAttribute]
[ComVisibleAttribute(true)]
public enum MethodAttributes
Visual C++
[SerializableAttribute]
[FlagsAttribute]
[ComVisibleAttribute(true)]
public enum class MethodAttributes
J#
/** @attribute SerializableAttribute */
/** @attribute FlagsAttribute */
/** @attribute ComVisibleAttribute(true) */
public enum MethodAttributes
JScript
public enum MethodAttributes
成员
成员名称 说明
MemberAccessMask 检索可访问性信息。
PrivateScope 指示该成员不能被引用。
Private 指示此方法只能由当前类访问。
FamANDAssem 指示此方法只能由该类型和它在此程序集中的派生类型的成员访问。
Assembly 指示此方法可由该程序集的任何类访问。
Family 指示此方法只可由该类及其派生类的成员访问。
FamORAssem 指示此方法可由任意位置的派生类访问,也可由程序集中的任何类访问。
Public 指示此方法可由任何包括该对象的对象访问。
Static 指示在类型上定义此方法,否则基于每个实例定义此方法。
Final 指示无法重写此方法。
Virtual 指示此方法为虚方法。
HideBySig 指示此方法按名称和签名隐藏,否则只按名称隐藏。
CheckAccessOnOverride 指示仅当此方法可访问时,才可以对其进行重写。
VtableLayoutMask 检索 vtable 属性。
ReuseSlot 指示此方法将重复使用 vtable 中的现有槽。这是默认行为。
NewSlot 指示此方法总是获取 vtable 中的新槽。
Abstract 指示此类不提供此方法的实现。
SpecialName 指示此方法是特殊的。名称描述此方法的特殊性。
PinvokeImpl 指示此方法的实现通过 PInvoke(平台调用服务)转发。
UnmanagedExport 指示此托管方法由 thunk 导出为非托管代码。
RTSpecialName 指示公共语言运行库检查名称编码。
ReservedMask 指示仅供运行时使用的保留标志。
HasSecurity 指示此方法具有关联的安全性。保留此标志仅供运行时使用。
RequireSecObject 指示此方法调用另一个包含安全性代码的方法。保留此标志仅供运行时使用。 示例
下面的示例显示指定方法的属性。Visual Basic 复制代码
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasicClass AttributesSample Public Sub Mymethod(ByVal int1m As Integer, ByRef str2m As String, ByRef str3m As String)
str2m = "in Mymethod"
End Sub 'Mymethod
Public Shared Function Main(ByVal args() As String) As Integer
Console.WriteLine("Reflection.MethodBase.Attributes Sample") ' Get the type of a chosen class.
Dim MyType As Type = Type.GetType("AttributesSample") ' Get the method Mymethod on the type.
Dim Mymethodbase As MethodBase = MyType.GetMethod("Mymethod") ' Display the method name and signature.
Console.WriteLine("Mymethodbase = {0}", Mymethodbase) ' Get the MethodAttribute enumerated value.
Dim Myattributes As MethodAttributes = Mymethodbase.Attributes ' Display the flags that are set.
PrintAttributes(GetType(System.Reflection.MethodAttributes), CInt(Myattributes))
Return 0
End Function 'Main Public Shared Sub PrintAttributes(ByVal attribType As Type, ByVal iAttribValue As Integer)
If Not attribType.IsEnum Then
Console.WriteLine("This type is not an enum.")
Return
End If
Dim fields As FieldInfo() = attribType.GetFields((BindingFlags.Public Or BindingFlags.Static))
Dim i As Integer
For i = 0 To fields.Length - 1
Dim fieldvalue As Integer = CType(fields(i).GetValue(Nothing), Int32)
If (fieldvalue And iAttribValue) = fieldvalue Then
Console.WriteLine(fields(i).Name)
End If
Next i
End Sub 'PrintAttributes
End Class 'AttributesSample
C# 复制代码
using System;
using System.Reflection;class AttributesSample
{
public void Mymethod (int int1m, out string str2m, ref string str3m)
{
str2m = "in Mymethod";
} public static int Main(string[] args)
{
Console.WriteLine ("Reflection.MethodBase.Attributes Sample"); // Get the type of the chosen class.
Type MyType = Type.GetType("AttributesSample"); // Get the method Mymethod on the type.
MethodBase Mymethodbase = MyType.GetMethod("Mymethod"); // Display the method name and signature.
Console.WriteLine("Mymethodbase = " + Mymethodbase); // Get the MethodAttribute enumerated value.
MethodAttributes Myattributes = Mymethodbase.Attributes; // Display the flags that are set.
PrintAttributes(typeof(System.Reflection.MethodAttributes), (int) Myattributes);
return 0;
}
public static void PrintAttributes(Type attribType, int iAttribValue)
{
if (!attribType.IsEnum) {Console.WriteLine("This type is not an enum."); return;} FieldInfo[] fields = attribType.GetFields(BindingFlags.Public | BindingFlags.Static);
for (int i = 0; i < fields.Length; i++)
{
int fieldvalue = (Int32)fields[i].GetValue(null);
if ((fieldvalue & iAttribValue) == fieldvalue)
{
Console.WriteLine(fields[i].Name);
}
}
}
}
这不是自相矛盾吗?请问访问是什么意思?
public可以赋值和取值
还有X不是属性,叫字段;
public class Test
{
public string x; //只是公有字段
};
写成这样:
public class Test
{
public string x;
public string HaHa //属性
{
get { return x; } //只要这个为直读属性
set { x = value; } //加这个,可以改变或赋值
}
};第二问:
可以,为什么不可以,只是类型不一样罢了.第三问:
[]指定属性或者事件.........在做CUSTOM控件的时候,里面会有很多.
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
Test fuck=new Test();
fuck.x = "1";
Console.WriteLine(fuck.x);
Console.ReadLine();
}
}
public class Test
{
public string x;
}
}这不是可以吗.最重要的是x定义为public(公共可访问的).就可以直接被其他类调用了.楼主如果把public string x;改为private string x;就不能访问了,因为这个时候变成私有的,不能被其他类访问了,所以调用不了fuck.x了.
回答第2问:(这里我直接把9楼的代码贴下来了,他输出时候,少了t.S[0],不然打不出字符来)
class Program
{
public static void Main(string[] args)
{
Test t = new Test();
t.S = new string[]{"s","sss"};
Console.WriteLine(t.S[0].ToString());
Console.ReadKey();
}
}
class Test
{
private string[] s; public string[] S
{
get { return s; }
set { s = value; }
}
}
回答第3问.属性或特性,也不是随便写的.
1.系统本身定义了一些固有的特性.比如
当要引用某个传统动态连接库中的函数时,我们常用到DllImportAttribute标准属性来注释某个函数的原型
[DllImport(“user32.dll”)]public static extern int MessageBoxA(int p, string m, string h, int t);//…
又比如,在编写Web Service时会使用到WebServiceAttribute和WebMethodAttribute属性。[WebService]和[WebMethod]会在生成WSDL时发挥作用,为我们生成相应的Web Service描述
[WebMethod]public string HelloWorld()//…
这些属性直接对程序集中的类型或类型成员进行注释,并且这样的注释可以影响相应程序集的编译、逻辑、运行等。.NET Framework带有很多标准的属性,并在MSDN中有详细的文档。
2.属性也可以自定义,即自行开发属性,对属性进行某种格式的定义,并利用属性来影响自己开发的程序.几乎所有的属性都继承自System.Attribute,我们自己定义的属性也需首先继承自该抽象类。这方面内容你可以查查msdn
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Test fuck = new Test();
fuck.x = "1";
Response.Write(fuck.x);
}
}
public class Test
{
public string x;
}
}
没有问题呀,也可以直接调用.在网页上用Response.Write做输出语句用.如果程序出错.可以把错误提示或者代码发上来.
还有楼上回答人测试没有啊public class TT
{
public string x;
} static void Main(string[] args)
{
TT t = new TT();
t.x = "22";
Console.WriteLine(t.x);
Console.Read();
}
是完全可以访问,并且可以赋值的呀,完全成功的
public class TT
{
public string x;
}
public class TT
{
public string x;
}
那你就写明白点啊[]里可以乱写啊你可写个ADC只要你写一个相应的ADCAttribute的类,并被他引用就可以了
也就是,如果你有一个以Attribute做为后缀的类,比如ABAttribute 写在 [AB]
SDFAttribute 写在 [SDF]不过好种这种类,好像要继承一个基类
在Test.cs里:
namespace WebApplication53
{
public class Test
{
public string x="2";
}
}
在Default.aspx页里:
Test aa = new Test();
Response.Write(aa.x);
----------------------------------------
ps:象string x="2";在C#里被称做字段.如果前面没有加修饰符,比如你没有定义为public 或者private等修饰符,则默认修饰符为private.例如string x="2";就等效于private string x="2";外部就访问不到.
不过平常建议还是让这字段声明为private(私有的),这样做可以提高数据的封装程度.
然后通过用属性get set对类中的数据进行访问.