在C#中,如何返回对象的属性所指的内容?

时间:2016-10-02 04:53:07

标签: c# .net oop reference

让我解释一下我的意思。说我有一个对象

Bar mybar = new Bar() { Reference = new Foo() { Val = 69 } }

和另一个像

mybar.Reference = null;

假设我有

var temp = mybar.Reference;
mybar.Reference = null;
mybar.Reference = temp;

我希望 暂时 设置

temp

然后将其恢复到以前的状态。好吧,我做不到

null

因为上述第2行将constructor(public af: AngularFire){ this.af.auth.subscribe(user => { console.log('---->', user) if (user) { this.user = user.auth.providerData[0]; if (user.auth.emailVerified==false){ user.auth.sendEmailVerification().then(function() { alert('Email Verification Sent!'); }); } else { this.router.navigate(['/dashboard']); } },error => { alert('dude whats up with this?') }); } onSubmit(){ console.log(this.email,this.password) this.af.auth.login({ email: this.email, password: this.password, }, { provider: AuthProviders.Password, method: AuthMethods.Password, }) } XCEPTION: Uncaught (in promise): Error: The email address is badly formatted.ErrorHandler.handleError @ error_handler.js:45next @ application_ref.js:273schedulerFn @ async.js:82SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ async.js:74onError @ ng_zone.js:120onHandleError @ ng_zone_impl.js:64ZoneDelegate.handleError @ zone.js:207Zone.runGuarded @ zone.js:113_loop_1 @ zone.js:379drainMicroTaskQueue @ zone.js:386ZoneTask.invoke @ zone.js:308 error_handler.js:50 ORIGINAL STACKTRACE:ErrorHandler.handleError @ error_handler.js:50next @ application_ref.js:273schedulerFn @ async.js:82SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ async.js:74onError @ ng_zone.js:120onHandleError @ ng_zone_impl.js:64ZoneDelegate.handleError @ zone.js:207Zone.runGuarded @ zone.js:113_loop_1 @ zone.js:379drainMicroTaskQueue @ zone.js:386ZoneTask.invoke @ zone.js:308 error_handler.js:51 Error: Uncaught (in promise): Error: The email address is badly formatted. at resolvePromise (zone.js:429) at resolvePromise (zone.js:414) at zone.js:462 at ZoneDelegate.invokeTask (zone.js:236) at Object.onInvokeTask (ng_zone_impl.js:34) at ZoneDelegate.invokeTask (zone.js:235) at Zone.runTask (zone.js:136) at drainMicroTaskQueue (zone.js:368) at HTMLFormElement.ZoneTask.invoke (zone.js:308)ErrorHandler.handleError @ error_handler.js:51next @ application_ref.js:273schedulerFn @ async.js:82SafeSubscriber.__tryOrUnsub @ Subscriber.js:223SafeSubscriber.next @ Subscriber.js:172Subscriber._next @ Subscriber.js:125Subscriber.next @ Subscriber.js:89Subject.next @ Subject.js:55EventEmitter.emit @ async.js:74onError @ ng_zone.js:120onHandleError @ ng_zone_impl.js:64ZoneDelegate.handleError @ zone.js:207Zone.runGuarded @ zone.js:113_loop_1 @ zone.js:379drainMicroTaskQueue @ zone.js:386ZoneTask.invoke @ zone.js:308 zone.js:355 Unhandled Promise rejection: The email address is badly formatted. ; Zone: angular ; Task: Promise.then ; Value: R {code: "auth/invalid-email", message: "The email address is badly formatted."} undefinedconsoleError @ zone.js:355_loop_1 @ zone.js:382drainMicroTaskQueue @ zone.js:386ZoneTask.invoke @ zone.js:308 zone.js:357 Error: Uncaught (in promise): Error: The email address is badly formatted.(…) 设置为Get-ChildItem E:\ -Recurse -Directory | where{$_.LastAccessTime.(Get-Date)-10 } 。那我怎么能做我想做的事呢?

1 个答案:

答案 0 :(得分:1)

不,你可以做到,它会起作用。

引用类型,如Foo所示,仅包含对实际对象的“引用”。因此,属性Bar.Reference包含Foo的实际对象的内存地址。

您的代码:

var temp = mybar.Reference;

上面的代码会将“内存地址/引用”复制到变量temp 现在,tempmybar.Reference都指向内存中的同一个对象。

mybar.Reference = null;

以上代码将变量mybar.Reference设置为null,现在mybar.Reference指向“无处”,但请注意,temp仍然具有对原始对象的引用。

mybar.Reference = temp;

最后一行将“{1}}的”内存地址“复制回temp